iOS rapid Development Framework Bee-framework application and Parsing (iii)---Message, Model, Signal

Source: Internet
Author: User

This time share beemessage, Beemodel, and beeuisignal. These three things are the main implementations of controller, Model, and event. You can also go to Bee's/documents/developer_manual.pdf to see the detailed development manuals, I hope you read this article can understand these components more deeply, suitable for what, so more handy to use. This article tries to answer several questions:

    • How is the Beemessage implemented? How to encapsulate the HTTP process, what are the pros and cons and where to apply?
    • How is the Beemodel implemented? What are the tradeoffs between using and core data? What are the ways to use it to meet different needs?
    • How is the beeuisignal implemented? To meet what needs to achieve this? What are the advantages and disadvantages of comparison with nsnotification and the applicable occasions?
A1:beemessage is the encapsulation of network data requests, which is often said to be "protocol." Beemessage and Beehttprequest (Asidataformrequest's encapsulation) work together to synchronize the status of Beehttprequest through the built-in multiple status updates. This includes sending and receiving requests, parsing the data, and sending the data to the upper-level object (UI or model) of interest. Beemessage and beehttprequest all hold responder data members, of which Beehttprequest responder is the corresponding beemessage, The beemessage corresponding responder is the UI or model that is interested in beemessage. See:
As you can see, Uiresponder will create beemessage, fill in the HTTP parameters, and pass yourself in as responder. Beemessage will pass himself into Beehttprequest. Uiresponder checks the status of Beemessage through the block provided by Beemessage. Both Beemessage and Beehttprequest are implemented as 6 sub-states, indicating the state flow of HTTP requests and synchronizing. These state flows are done in Beemessage's routine function. Take a look at the code for Uiresponder, and beemessage routine:
Uiresponder:
-(void) getnewslist {    beemesage * API = [Api_news_list API];
   
    Adding HTTP Parameters
    Api. INPUT (@ "UID", [NSString stringwithformat:@ "%d", _uid]); <p style= "margin-top:0px; margin-bottom:0px; font-size:11px; Font-family:menlo; " ></p>    api.whenupdate = ^    {                if (api.sending)        {            //process send        }        else if (api.succeed)        {
            Send successfully, get beemessage analytic good data            [Self.news addObjectsFromArray:api.resp.news];        }        else if (api.failed)        {
             Processing failed        }        else if (api.cancelled)        {
             Processing cancellation        }    };        [API send];
}

Beemessage routine Function:
-(void) routine{    if (self.sending)    {        NSString * requesturi = [[[Serverconfig sharedinstance] url] Stringby Appendingstring:api_news_list];                Self. Http_get (RequestUri);    }    else if (self.succeed)    {        nserror* error;
        Parse palindrome        cxmldocument * document = [[Cxmldocument alloc] initWithXMLString:self.responseString options by xml: Nsutf8stringencoding error:&error];                nsarray* newslist = [Document nodesforxpath:@ "Oschina/newslist/news" error:&error];                       For (cxmlelement* newsxml in newslist)        {            if (newsxml = = nil) continue;                        news* news = [news createbyxml:newsxml];            [Self.resp.news addobject:news];        }                if (nil = = Self.resp | | NO = = [Self.resp validate])        {            self.failed = YES;            return;        }    }    else if (self.failed)    {    }    else if (self.cancelled)    {    }} @end

The above is the Beemessage workflow. The advantage of beemessage is that it greatly simplifies the writing of data protocols, omits delegate, blocks that are scattered throughout the code, and keeps the code well maintained. The beehttprequest encapsulates the JSON kit, which makes it very easy to convert a palindrome into a nested dictionary, and the XML needs to write its own parser. In the actual app often write dozens of such beemessage is very boring,Bee provides tools for automating the generation of Beemessage code in/tools/scaffold, supporting JSON data.
     A2:beemodel is the base class for local data storage, with observer support included. Beemodel observers are generally UI, holding this beemodel, such as the above news list data, the UI is the Viewcontroller of the news list display. Beemodel requires the developer to implement local storage in the subclass, the function is Savecache, Loadcache. Bee realizes the Plist-based Beeuserdefaults, realizes the file-based Beefilecache, realizes the Beememcache based on the memory dictionary or the array, Beeimagecache, can use flexibly according to the demand. You can also use Beedatabase and Beeactiverecord in Beemodel to support local database storage, as described in the developer's Manual. Why did Beemodel introduce the viewer? Of course, in order to simplify the delegate and notification, from the code to see the feeling should be for the beeuisignal to do the groundwork. Beeuisignal's routing method is implemented by reflection, and for Beemodel, the Observer's nsclass can be deposited into the cache, and when routed events are cached to find out if these observers have implemented the receiving method of the event and sent it preferentially, this can speed up the efficiency of the event routing. Beemodel is only the base class of the data model and can encapsulate a nsmutablearray that is stored by Nsmutablearray for specific data objects. Core data individuals think of only sqllite encapsulation, which is used to process data associated scenarios. Apple probably introduced core data to OS X in 2004 and migrated to iOS, which seems to be not very popular with developers. Beemodel processing local storage is more flexible, providing convenient uisignal events, more practical. You can use Beeviewmodel that supports beeuisignal when you use it. Beeviewmodel contains a number of subclasses, mainly the abstraction of several typical UI presentation methods. Beeonceviewmodel a display of data, Beepageviewmodel support page management, Beestreamviewmodel support waterfall management, easy to use. For example, Beestreamviewmodel, only implement FirstPage, and nextpage two methods, and in the model data update when the event to Uiviewcontroller can be sent. The code is as follows:
        
-(void)        firstpage{_pages = 0;        [Api_post_list Cancel];   Api_post_list *API = [Api_post_list API];   
    20 APIs per page.    INPUT (@ "PageIndex", @ "0"); Api.        INPUT (@ "pageSize", @ "20");                Api.whenupdate = ^ {@normalize (API); if (api.sending) {[Self senduisignal:self.        Reloading];                            } else if (api.succeed) {//First page fetch succeeded [self.posts Removeallobjects];                            [Self.posts addObjectsFromArray:api.resp.posts];            self.loaded = YES;                            Self.pages = 1; [Self senduisignal:self.        RELOADED]; } else if (api.failed) {[Self senduisignal:self.        RELOADED];        }    }; [API send];}        -(void) nextpage{[api_post_list Cancel];        Api_post_list *API = [Api_post_list API];    int curbegin = self.pages * 20;    int curend = Curbegin + 20; Api.    INPUT (@ "PageIndex", [NSString stringwithformat:@ "%d", Curbegin]); Api.      INPUT (@ "PageSize", [NSString stringwithformat:@ "%d", curend]);  Api.whenupdate = ^ {@normalize (API); if (api.sending) {[Self senduisignal:self.        Reloading]; } else if (api.succeed) {
            Process the next page            if (_pages = = 0)            {                [self.posts removeallobjects];            }                        [Self.posts addObjectsFromArray:api.resp.posts];                     self.loaded = YES;            Self.pages  = self.pages + 1;            [Self senduisignal:self. RELOADED];                }        else if (api.failed)        {            [self senduisignal:self. FAILED];        }        else if (api.cancelled)        {            [self senduisignal:self. CANCELLED];        }    ;        [API send];}

It's easy to complete the waterfall paging process, isn't it? When appropriate, send reloading and reloaded events to the UI, UI update interface is available.
A3:beeuisignal is a UI event implemented by Bee, with the following features:
    • An object-specific event that specifies the source, target
    • Can carry Attachment object
    • The event object has a three-segment naming method with namespaces. For example: "Signal. Newsmodel.reloaded ", which represents a RELOADED event originating from the Newsmodel type.
    • The event object and the receiving end are decoupled, and the receiver uses the "fixed prefix + Event name" method to generate selector, such as-(void) handleuisignal_newsmodel_reloaded: (beeuisignal*) signal;
    • The normal UI object can implement the Signaltarget method by itself and decide the forwarding path of the uisignal, for example, UIView's forwarding path is the corresponding [view Viewcontroller];
beeuisignal through Beeuisignalbus forwarding, specific forwarding order is more complex, interested classmates can debug code comb clear. However, there are only four options for sending the event: try to give the target class, try to give observer classes, try to give UIView Superview, try to give a class signaltarget ( UIView's Signaltarget is Uiviewcontroller). With these four options, the Beeuisignal solves both the directed send (in lieu of delegate), the Observer Send (Nsnotification), and the UIView Responder send (UI Responder Chain). An event that blends with Apple's usual event-sending support.
It can be seen that the beeuisignal is more widely used than the nsnotification, and the artificial fusion of various communication modes. Writing because of the macro is also relatively simple, the specific wording can refer to the developer's Manual. It can be seen that beeuisignal brings a lot of benefits, introduced a complete and slightly complex mechanism. If it is not an event delivery for the UI, or a simple event notification with an attachment, the individual considers it unnecessary to introduce beeuisignal and solve the problem with Nsnotification and delegate.         

iOS rapid Development Framework Bee-framework application and Parsing (iii)---Message, Model, Signal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.