You should not call self directly in your block. It may lead to a retain cycle. So before the block declare:
__weak typeof(self) weakSelf = self;
And use weakSelf
in your block instead of self.
문제
I have a UITextField which lets the user enter a custom Album name for Facebook Photos. Once textFieldShouldReturn, I take the textField.text and send it to Facebook. Once the completion handler is called, I would like to update my model and reload the data in my tableView.
My problem is that some lines of my completionHandler are never called. I marked all lines with the debugger and once I step through the code line-by-line, some of these lines are never called, especially not the tableView reload method.
Am I doing something wrong thread-wise? Why is my completionHandler in parts cancelled and never called?
Here's my code:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
NSString *newAlbumName;
newAlbumName = textField.text;
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setObject:newAlbumName forKey:@"name"];
[parameters setObject:newAlbumName forKey:@"message"];
FBRequest* request = [FBRequest requestWithGraphPath:@"me/albums" parameters:parameters HTTPMethod:@"POST"];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:request
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSDictionary *newAlbum;
[newAlbum setValue:newAlbumName forKey:@"name"];
[self.albums addObject:newAlbum];
NSLog(@"created: %@", [newAlbum objectForKey:@"name"]);
[self.tableView reloadData];
}
else {
NSLog(@"Error: %@", error.userInfo);
}
}];
[connection start];
[textField resignFirstResponder];
[textField removeFromSuperview];
return YES;
}
해결책
You should not call self directly in your block. It may lead to a retain cycle. So before the block declare:
__weak typeof(self) weakSelf = self;
And use weakSelf
in your block instead of self.
다른 팁
I believe that this is threading issue. I never worked with FBRequestConnection
class but if its block is executed on the background thread you need to update your UI on the main thread like this:
if (!error) {
NSDictionary *newAlbum;
[newAlbum setValue:newAlbumName forKey:@"name"];
[self.albums addObject:newAlbum];
NSLog(@"created: %@", [newAlbum objectForKey:@"name"]);
dispatch_async(dispatch_get_main_queue(),^{
[self.tableView reloadData];
});
}