2
我使用NSFetchedResultController
在我app.But NSFetchedResultController delegate
方法时,我插入新的记录不会被调用core data
之中,但它工作时,我试图删除或更新现有records.Here是我的代码 -NSFectchedResultController委托不被调用?
- (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)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)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:@"Please enter friends's ID."];
return;
}
NSString* userid = [[NSUserDefaults standardUserDefaults] valueForKey:@"userId"];
NSString* req = [JSONCreator getAddFriendEnc:myId andFid:fid];
if ([fid isEqualToString:userid]) {
[AppDelegate showInfoAlert:@"Please 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];
}
}];
}
}
数据被保存到数据库,但表视图不会reloaded.what我做错了吗?任何建议,将不胜感激。
使用了'self.fetchedResultsController'在吸气的原因吗?我只是在那里到处使用'_fetchedResultsController'。 – Fogmeister
总是很好,先排除简单的事情我猜..你确定你正在NSFetchedResultsController正在使用相同的NSManagedObjectContext创建新的对象吗? (考虑到它不适用于插入,但是在删除或更新时) – oehman
您也可以尝试删除谓词,并查看是否更改了任何内容。 – oehman