我有一个发送请求并有一个委托响应者的方法。该方法由 KVO 响应程序调用。当我从 viewDidLoad 调用它时,代码工作正常,但是当我从 KVO 方法发出请求时,它不会触发委托方法。
这是我的 KVO 响应者:
//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];
}
}
及其调用的方法:
-(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");
}
和委托方法:
- (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];
}
在这种情况下不会调用委托方法,但是当我直接从控制器调用 -(void)FBFriendSuggester 时它确实可以正常工作。我不知道该怎么做,我尝试设置一个通知来响应,希望它会触发委托,但这也不起作用。