NSNotificationCenter - Way to wait for a notification to be posted without blocking main thread?

≯℡__Kan透↙ 提交于 2020-01-15 20:16:15

问题


I'm using an AFNetworking client object which makes an asynchronous request for an XML document and parses it.

Also using NSNotificationCenter to post a notification when the document has finished parsing.

Is there a way to wait for a notification to be posted without blocking the main thread?

E.g code:

-(void)saveConfiguration:(id)sender {

    TLHTTPClient *RESTClient = [TLHTTPClient sharedClient];

    // Performs the asynchronous fetching....this works. 
    [RESTClient fetchActiveUser:[usernameTextField stringValue] withPassword:[passwordTextField stringValue]];

    /*
     * What do I need here ? while (xxx) ?
     */

    NSLog(@"Fetch Complete.");

}

Basically I'm wondering what sort of code I need in the above specified area to ensure that the function waits until the fetch has been completed ?

As it is right now I'll see "Fetch Complete." in the debug console before the fetch has been completed.

I tried adding a BOOL flag to the TLHTTPClient class:

BOOL fetchingFlag;

and then trying:

while([RESTClient fetchingFlag]) { NSLog(@"fetching...); } 

When this class receives the notification it sets RESTClient.fetchingFlag = FALSE; which should technically kill the while loop right? Except my while loop runs infinitely ?!


回答1:


Basically I'm wondering what sort of code I need in the above specified area to ensure that the function waits until the fetch has been completed ?

You need no code. Don't put anything in the method after you start the fetch, and nothing will happen. Your program will "wait" (it will actually be processing other input) until the notification is recieved.

In the notification handler method, put all the stuff that you need to do when the fetch is completed. This is (one of) the point(s) of notifications and other callback schemes -- your object won't do anything further until it gets the notification that it's time to act.

Is there a way to wait for a notification to be posted without blocking the main thread?

That's exactly how it works already.




回答2:


If you don't need to inform multiple objects upon completion of the task, you could add a completion handler (objc block) to the -fetchActiveUser:withPassword: method (so it would become something like -fetchActiveUser:withPassword:completionHandler: and add the code to be executed in the completion handler.

An example, lets say your -fetchActiveUser:withPassword:completionHandler: method looks like the following:

- (void)fetchActiveUser:(NSString *)user 
           withPassword:(NSString *)pass 
      completionHandler:(void (^)(TLUser *user, NSError *error))handler
{
    NSURL *URL                   = [NSURL URLWithString:@"http://www.website.com/page.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];    
    NSOperationQueue *queue      = [NSOperationQueue currentQueue];
    [NSURLConnection sendAsynchronousRequest:request 
                                       queue:queue 
                           completionHandler:^ (NSURLResponse *response, NSData *data, NSError *error) 
    {
        if (!handler) 
        { 
            return 
        };

        if (data) 
        {
            TLUser *user = [TLUser userWithData:data];

            if (user)
            {
                handler(user, nil);
            }
            else
            {
                NSError *error = // 'failed to create user' error ...
                handler(nil, error);
            }
        }
        else
        {
            handler(nil, error);
        }
    }];
}

The completion handler will be called whenever the task is finished. It will either return a TLUser object or an Error if something went wrong (bad connection, data format changed while parsing, etc...).

You'll be able to call the method like this:

- (void)saveConfiguration:(id)sender 
{
    TLHTTPClient *RESTClient = [TLHTTPClient sharedClient];


    // Performs the asynchronous fetching

    [RESTClient fetchActiveUser:[usernameTextField stringValue] 
                   withPassword:[passwordTextField stringValue] 
              completionHandler:^ (TLUser *user, NSError *error)
    {
        if (user)
        {
            NSLog(@"%@", user);
        }
        else
        {
            NSLog(@"%@", error);    
        }
    }];
}

Of course, in this example I've used the build in asynchronous methods of NSURLConnection in stead of AFNetworking, but you should be able to get the general idea.



来源:https://stackoverflow.com/questions/10231157/nsnotificationcenter-way-to-wait-for-a-notification-to-be-posted-without-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!