关闭应用程序后,某些CoreData关系会丢失 [英] Some CoreData relationships are lost after closing app

查看:101
本文介绍了关闭应用程序后,某些CoreData关系会丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的概述:

我添加(并确认它们添加)从soap服务加载到CoreDat的1400关系。在我关闭应用程序并打开它后,一些关系失去了;我只看到大约800(尽管它不同)。

I am adding (and confirm they are added) about 1400 relationships loaded from a soap service into CoreDat. After I close the app and open it again some of the relationships are lost; I only see around 800 of them (although it varies). Also, I am not getting any errors.

现在,更多详细信息:

我有一个对象用户具有用户保存的服务的信息;它看起来像这样:

I have an object called User that has information about services a user have saved; it looks something like this:

@interface OosUser : NSManagedObject

    + (OosUser *) userFromSlug: (NSString *) slug;
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *slug;
    @property (nonatomic, retain) NSMutableSet /* Service */ *services;
    - (void) addServicesObject: (Service * )service;
    - (void) removeServicesObject: (Service *) service;

@end

@implementation User

   @dynamic name;
   @dynamic slug;
   @dynamic services;

   static NSString *fetchPredicate = @"slug = %@";

   + (User *) userFromSlug:(NSString *)slug
   {
       User *result = [super objectWithPredicate: fetchPredicate, slug];
       if (!result) {
           result = [super create];
           result.slug = slug;
       }
   return result;
   }

@end

使用数据时,关系保存如下:

In the part of the code where the data is used, the relationships are saved like this:

NSMutableSet *userServices = self.user.services;

for (Service *service in servicesToAdd) {
        [self.services addObject: service];
        bool contained = false;
        for (Service *userService in userServices) {
            if ((contained = [userService.slug isEqualToString:service.slug])) {
                break;
            }
        }
        if (!contained) {
            // [userServices addObject:service];
            [self.user addServicesObject: service];
            NSError *error = nil;
            if (![[service managedObjectContext] save:&error]) {
                NSLog(@"Saving failed");
                NSLog(@"Error: %@", [error localizedDescription]);
            }else {
                NSLog(@"Registered service %d: %@", self.services.count, service.slug);
            }
        }
    }

用调试器检查,我可以看到,所有1400以上的关系被添加,但当应用程序被重置,并通过self.user.services恢复只有大约800个对象。

The case is that I have checked with the debugger and I can see that all the over 1400 relationships are added, but when the app is reset and they are restored though self.user.services I only get around 800 objects.

为什么会发生这种情况?

Why could this be happening? Anybody had this before?

UPDATE:

人们一直在建议我没有正确使用Core Data,但问题是重新启动应用程序后数据丢失。使用它时绝对没有问题。

People keep suggesting that I am not using Core Data correctly but the problem is that the data is lost AFTER restarting the app. There is absolutely no problem with it while using it. I am using Core Data as correct as it could be given the limited documentation and examples you get from Apple.

推荐答案

NSMutableSet *userServices = self.user.services;

...

            [userServices addObject:service];

不能这样做。 self.user.services 不返回可变集。这就是 addServicesObject 方法的用法。将第二行更改为:

Can't do that. self.user.services does not return a mutable set. That's what that addServicesObject method is for. Change the second line to:

            [self.user addServicesObject:service];

从Apple文档:


重要的是要理解点访问器返回值和 mutableSetValueForKey:之间的区别。 mutableSetValueForKey:返回一个可变的代理对象。如果您改变其内容,它将为该关系发出适当的键值观察(KVO)更改通知。点访问器简单地返回一个集合。如果您按照此代码片段中所示操作集合:

It is important to understand the difference between the values returned by the dot accessor and by mutableSetValueForKey:. mutableSetValueForKey: returns a mutable proxy object. If you mutate its contents, it will emit the appropriate key-value observing (KVO) change notifications for the relationship. The dot accessor simply returns a set. If you manipulate the set as shown in this code fragment:

[aDepartment.employees addObject:newEmployee]; // 不要这样做!
然后KVO更改通知不发出,并且反向关系没有正确更新。

回想一下,点只是调用访问器方法,所以出于同样的原因:

Recall that the dot simply invokes the accessor method, so for the same reasons:

[[aDepartment employees] addObject:newEmployee]; //不要这样做!

这篇关于关闭应用程序后,某些CoreData关系会丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆