ios - 合并托管对象上下文后,托管对象属性变为 nil

标签 ios cocoa core-data

我有一个名为 SpecialItem 的托管对象,并调用 setSubcategory 来更改子类别。当我保存临时上下文并与主上下文合并时,以某种方式调用 setSubcategory 并传入 nil 作为子类别。这通常会导致保存 SpecialItem 对象时将 myProperty 设置为 nil。我不知道什么是调用setSubcategory。我没有明确调用 setSubcategory:nil。

我的问题是,发生了什么以及如何解决这个问题?

这是托管对象实现:

@interface SpecialItem : GenericItem
@property (nonatomic, strong) Subcategory *subcategory;
@property (nonatomic, strong) MyProperty *myProperty;
@end

@implementation SpecialItem
@dynamic subcategory;
@dynamic myProperty;

- (void)setSubcategory:(Subcategory *)subcategory
{
   [self willChangeValueForKey:@"subcategory"];
   [self willChangeValueForKey:@"myProperty"];

   [self setPrimitiveValue:subcategory forKey:@"subcategory"];
   [self setPrimitiveValue:subcategory.category.myProperty forKey:@"myProperty"];

   [self didChangeValueForKey:@"myProperty"];
   [self didChangeValueForKey:@"subcategory"];
}
// ...

托管对象上下文的设置如下:

self.tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.tempContext.parentContext = self.dataManager.mainContext;

最终我得到了这个:

[self saveTempContext];

这是 saveContext 实现:

- (void)saveContext
{
   LogAndPrint(@"Before save.");
   [self.tempContext performBlockAndWait:^{
      NSError *error = nil;
      if (![self.tempContext save:&error])
      {
         LogAndPrint(@"Error occurred while saving context: %@", error);
      }
   }];

   LogAndPrint(@"Middle of save.");

   [self.dataManager.mainContext performBlockAndWait:^{
      NSError *error = nil;
      if (![self.dataManager.mainContext save:&error])
      {
         LogAndPrint(@"Error occurred while saving context: %@", error);
      }
   }];

   [self.dataManager synchronize];
   LogAndPrint(@"After save.");
}

这是同步实现:

- (void)synchronize
{
   LogAndPrint(@"Synchronizing Core Data changes.");
   if (self.persistentContext.hasChanges) {
      [self.persistentContext performBlockAndWait:^{
         NSError *error =  nil;
         if (![self.persistentContext save:&error]) {
            LogAndPrint(@"Error occurred while saving persistent context: %@", error);
         }
      }];
   }
}

最佳答案

我一直无法弄清楚为什么会发生这种情况。但我确实找到了一个有效的解决方案。我更改了调用 setSubcategory 的代码以调用名为 [SpecialItem updateSubcategory:subcategory] ​​的新方法。

- (void)updateSubcategory:(Subcategory *)subcategory
{
   self.subcategory = subcategory;
   self.myProperty = subcategory.category.myProperty;
}

这解决了这个问题,并且代码已经正常运行了几个月。

关于ios - 合并托管对象上下文后,托管对象属性变为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030793/

相关文章:

ios - 如何在 ios 13 中显示的 View Controller 下方设置系统背景颜色

ios - 如何将新对象插入到实体中并设置该对象与另一个实体的现有对象的关系?

iphone - 我如何更改 UILocalNotification 中的 UserInfo?

objective-c - 如何允许用户在不暂停应用程序更新循环的情况下调整 NSSlider?

iphone - 设备功能编程指南去了哪里?

swift - cocoa swift : Launch At Login not working & throws error in Xcode?

cocoa - 在应用程序文件夹中找不到 Mac 应用程序

cocoa - 设置包括子实体 : in an NSFetchRequest is broken for entities across multiple persistent stores

ios - 我如何检查存储在我的核心数据数据库中的内容?

ios - 如何在 Swift 的另一个 ViewController 中打印选定的单元格数据?