首页 新闻 会员 周边 捐助

Coredata 未能更新数据

0
悬赏园豆:50 [已关闭问题] 关闭于 2016-11-09 14:37

Hi, 大神们:

我用Coredata 可以保存、查询数据,但不能更新数据。

下面是我的代码:

 1 //Fragment 1
 2 NSFetchRequest* request = self.fetch;
 3                 request.predicate = [NSPredicate predicateWithFormat:@"object_id=%@",notificationItem[@"object_id"]]; //object_id = issue_id
 4                 NSArray *coredataNotificationsArray = [self.managedObjectContext executeFetchRequest:request error:&error];
 5                 
 6                 NotificationEntity *notiEntity = [[NotificationEntity alloc] initWithDict:notificationItem];
 7 
 8 for (NotificationEntity *coredataEntity in coredataNotificationsArray) {
 9                         AudioServicesPlaySystemSound(1007);//Player Message Alert.
10                         
11                         NSInteger oldMessageIndex = [self findMessageInDatasourceAtIndex:notificationItem];
12                         
13                         if(oldMessageIndex != -1) //if exist in the current memory datasource[messageArray];
14                         {
15                             //2.1 update UI.
16                             [notificationDatasourceArray replaceObjectAtIndex:oldMessageIndex withObject:notiEntity];
17                             [self updateNotifiAtIndex:oldMessageIndex];
18                         }
19                         
20                         // Same issue id but send_at not same
22                         [NotificationEntity notification:coredataEntity AssignWith:notiEntity];
23                         
24                         if (![self.managedObjectContext save:&error]) {
25                             NSLog(@"未能成功保存数据");
26                         }
27                     }
28 
29 
30 
31 
32 //Method 1
33 - (NSFetchRequest *)fetch
34 {
35     NSFetchRequest *request = [[NSFetchRequest alloc] init];
36     NSEntityDescription *entity = [NSEntityDescription entityForName:GT_TB_Notification inManagedObjectContext:self.managedObjectContext];
37     
38     //NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults] dictionaryForKey:GT_UD_MyInfo];
39 //    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"receiver_id=%@",userInfo[@"id"]];
40 //    request.predicate = predicate;
41     
42     [request setEntity:entity];
43     [request setReturnsObjectsAsFaults:NO];
44     
45     return request;
46 }
47 
48 //Method2
49 + (void)notification:(NotificationEntity *)manageObject AssignWith:(NotificationEntity *)entity
50 {
51     [manageObject setContent: entity.content];
52     manageObject.count = entity.count;
53     manageObject.gid = entity.gid;
54     manageObject.issue_content = entity.issue_content;
55     manageObject.object_id = entity.object_id;
56     
57     manageObject.object_kind = entity.object_kind;
58     manageObject.receiver_header = entity.receiver_header;
59     manageObject.receiver_id = entity.receiver_id;
60     manageObject.receiver_name = entity.receiver_name;
61     manageObject.send_at = entity.send_at;
62     
63     manageObject.sender_header = entity.sender_header;
64     manageObject.sender_id = entity.sender_id;
65     manageObject.sender_name = entity.sender_name;
66     manageObject.title = entity.title;
67     manageObject.status = entity.status;
68     
69     manageObject.issue_status = entity.issue_status;
70     manageObject.is_read = entity.is_read;
71 }

 

这里的 coredataEntity,只修改了 count 字段,可以看到coredataEntity的count,赋值前由1变成了7,但是访问 save 方法后,Database里的数据还是没变,重新运行程序的时候,还是以前的值。

当我访问[self.manageObjectContext hasChange] 的时候返回NO,但是调试的时候又可以看到coredataEntity.count 由1 变成了 7 ,说明是变化了。但就是保存不起数据,就求大神帮我...

JEREMYS的主页 JEREMYS | 初学一级 | 园豆:152
提问于:2015-04-16 12:53
< >
分享
所有回答(1)
0

我就看看。

Firen | 园豆:5385 (大侠五级) | 2015-04-16 17:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册