我在我的应用程序中使用 NSFetchedResultController 和 core data 。但是当我插入新记录时,不会调用 NSFetchedResultController 委托(delegate) 方法,但是当我尝试删除或更新现有记录时它会起作用。这是我的代码 -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//fetch friends from DB
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
self.managedObjectContext = appDelegate.managedObjectContext;
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName"Friends" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"status == 0"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey"userId" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
这个用于保存/更新/删除 -
-(void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex{
if (alertView.tag == 1001) {
if (buttonIndex == 0) { // add new friend
NSString* fid = [[alertView textFieldAtIndex:0].text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (fid.length == 0) {
[AppDelegate showInfoAlert"lease enter friends's ID."];
return;
}
NSString* userid = [[NSUserDefaults standardUserDefaults] valueForKey"userId"];
NSString* req = [JSONCreator getAddFriendEnc:myId andFid:fid];
if ([fid isEqualToString:userid]) {
[AppDelegate showInfoAlert"lease enter your friends's ID."];
return;
}
[ConnectionManager invokeAsync"GET" body:req request"/friends" viewForHud:self.view completion:^(NSDictionary* res, NSString* error){
if (error) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle"Error!" message:error delegate:Nil cancelButtonTitle"OK" otherButtonTitles:Nil, nil];
[alert show];
}
else
{
Friends * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Friends" inManagedObjectContext:managedObjectContext];
newEntry.userId = userid;
newEntry.friendId = fid;
newEntry.status = @"0";
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
//[managedObjectContext insertObject:newEntry];
//[self.frindsTable reloadData];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:[res valueForKey:@"response"] delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
[alert show];
}
}];
}
}
else if (alertView.tag == 1002){ // delete friend
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:selectedIndexPath];
[managedObjectContext deleteObject:managedObject];
[managedObjectContext save:nil];
[NSFetchedResultsController deleteCacheWithName:nil];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Friend removed from your friend list." delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:Nil, nil];
[alert show];
}
else if (alertView.tag == 1003){ // update friend
NSString* req = [JSONCreator getBlockFriendEnc:myId andFid:frndtoBlock];
[ConnectionManager invokeAsync:@"GET" body:req request:@"/friends" viewForHud:self.view completion:^(NSDictionary* res, NSString* error){
if (error) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:error delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
[alert show];
}
else
{
NSString* userid = [[NSUserDefaults standardUserDefaults] valueForKey:@"userId"];
Friends* newFriend;
NSError * error;
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Friends"
inManagedObjectContext:managedObjectContext]];
[fetchRequest setFetchLimit:1];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(userId == %@) && (friendId == %@)",userid, frndtoBlock]];
if ([managedObjectContext countForFetchRequest:fetchRequest error:&error])
newFriend = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] lastObject];
newFriend.status = @"1";
// save
if (! [managedObjectContext save:&error])
NSLog(@"Couldn't save data to %@", NSStringFromClass([self class]));
[NSFetchedResultsController deleteCacheWithName:nil];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Friend blocked from your friend list." delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:Nil, nil];
[alert show];
}
}];
}
}
数据已保存到数据库但未重新加载 TableView 。我做错了什么?任何建议将不胜感激。
Best Answer-推荐答案 strong>
我猜是问题
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"status == 0"];
用 改变它
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"status == '0'"];
希望这会有所帮助..
关于ios - 未调用 NSFectchedResultController 委托(delegate)?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/22688757/
|