• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 未调用 NSFectchedResultController 委托(delegate)?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 20:51:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在我的应用程序中使用 NSFetchedResultControllercore 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-推荐答案


我猜是问题

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"status == 0"];

改变它
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"status == '0'"];

希望这会有所帮助..

关于ios - 未调用 NSFectchedResultController 委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22688757/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap