MagicalRecord Find(All)有时无法从关系中找到对象

时间:2014-04-16 07:59:33

标签: ios core-data magicalrecord

当我关闭模拟器并重新打开它或推动并弹出一些视图控制器时,关系中的对象就消失了。当我第一次将对象保存到核心数据时,我仍然可以获取对象。

这是将对象保存到核心数据的代码:

for (NSDictionary *obj in results) {
    Stock *stock = [Stock MR_createEntity];
    stock.name = obj[@"name"];
    NSArray *categories = obj[@"categories"];
    if ([categories count] > 0) {
        for (NSDictionary *category in categories) {
            StockCategory *stockCategory = [StockCategory MR_createEntity];
            stockCategory.categoryId = category[@"_id"];
            stockCategory.name = category[@"name"];
            [stock addCategoriesObject:stockCategory];
        }
    }
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
    self.stocks = [Stock MR_findAllSortedBy:@"createdDate" ascending:NO];
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self.tableView reloadData];
    });
}];

下面的代码是从核心数据中检索对象:

self.stocks = [Stock MR_findAllSortedBy:@"createdDate" ascending:NO];
dispatch_async(dispatch_get_main_queue(), ^{
    [SVProgressHUD dismiss];
    [self.tableView reloadData];
});

如果我关闭模拟器并重新启动它,我从核心数据中获取对象,但股票的StockCategory数组为空。我记得当我第一次将股票保存到核心数据时,股票的StockCategory数组不为空。为什么当我重新启动模拟器时,关系中的对象就消失了。

模特: Stock.h

@class StockCategory;

@interface Stock : NSManagedObject

@property (nonatomic, retain) NSString * name;

@property (nonatomic, retain) NSSet *categories;
@end

@interface Stock (CoreDataGeneratedAccessors)

- (void)addCategoriesObject:(StockCategory *)value;
- (void)removeCategoriesObject:(StockCategory *)value;
- (void)addCategories:(NSSet *)values;
- (void)removeCategories:(NSSet *)values;

@end

StockCategory.h

@class Stock;

@interface StockCategory : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Stock *stock;

@end

0 个答案:

没有答案