简体   繁体   中英

iOS delegate issues with KVO

I have a method which sends out a request and has a delegate responder. The method is being called by a KVO responder. The code works fine when I call it from viewDidLoad, but when I make the request from the KVO method, it doesn't trigger the delegate method.

here is my KVO responder:

//observe app delegates userdata to check for when user data is available or data is changed
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"userData"] )
    {
        //goto facebook logic
        [self FBFriendSuggester];
    }
}

and the method it calls:

-(void)FBFriendSuggester
{
    NSDictionary * fb_credentials = nil;
    if([prefs objectForKey:@"facebookCredentials"])
    {
        fb_credentials = [prefs objectForKey:@"facebookCredentials"];
        if ([fb_credentials objectForKey:@"expiry"] && [fb_credentials objectForKey:@"fbtoken"]) {
            NSLog(@"found stored Facebook credentials.");
            ApplicationDelegate.facebook.accessToken = [fb_credentials objectForKey:@"fbtoken"];
            ApplicationDelegate.facebook.expirationDate = [fb_credentials objectForKey:@"expiry"];
        }
    }
    if ([ApplicationDelegate.facebook isSessionValid]) {
        printf("_valid facebook\n");
        [ApplicationDelegate.facebook requestWithGraphPath:@"me/friends" andDelegate:self];
    }else
        printf("_invalid facebook\n");
}

and the delegate method:

- (void)request:(FBRequest *)request didLoad:(id)result{
    NSLog(@"result2:%@",result);

    NSDictionary * rawObject = result;
    NSArray * dataArray = [rawObject objectForKey:@"data"];

    for (NSDictionary * f in dataArray) {
        [friends addObject:[[KNSelectorItem alloc] initWithDisplayValue:[f objectForKey:@"name"]
                                                            selectValue:[f objectForKey:@"id"]
                                                               imageUrl:[NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=square", [f objectForKey:@"id"]]]];
    }
    [friends sortUsingSelector:@selector(compareByDisplayValue:)];
    [self pickerPopup:self];
}

the delegate method doesn't get called in this scenario, but it does work fine when i call -(void)FBFriendSuggester directly from the controller. I don't know what to do, i tried setting a notification to respond in hopes that it would trigger the delegate, but that didn't work either.

I know this is rather old, but to give a reason for this happening, KVO notifications are sent on the thread that the change occurs on. So if a background thread makes a change to the property on the AppDelegate, the notification change will occur on the background thread as well.

So, in the context of your code, your -(void)FBFriendSuggester is most likely getting called in a background thread. You could confirm this by tossing a breakpoint in the code and looking at the Debug Navigator.

I'll continue with the assumption that this was true (It was running in a background thread).

This call, [ApplicationDelegate.facebook requestWithGraphPath:@"me/friends" andDelegate:self]; most likely needs to occur on the main thread. Reason for this is probably the underlying calls are using an NSURLConnection to make the call. The connections are made on background threads, but all delegate calls MUST be made back on the main thread, NOT a background thread. With the call andDelegate:self , its registering the object as a delegate on the background thread, instead of the main thread.

This is why your current solution fixes that issue.

Happy Coding!

I was able to work around my problem by issuing:

[self performSelectorOnMainThread:@selector(FBFriendSuggester) withObject:nil waitUntilDone:NO];

It would be great to know why this works like this though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM