我正在尝试将数据数组保存到核心数据中。 json就像:
[
{
"title": "iPad mini",
"_id": "5376d5215c4abd5505f34104",
"__v": 0,
"stocks": [
{
"name": "dosh",
"_id": "5376022ba31c4b8ca8c45e30",
},
{
"_id": "5375ff3ac91242b2a59d4784",
"name": "iPad mini ",
}
],
"createdDate": "2014-05-17T03:18:57.432Z"
},
{
"title": "ipad",
"_id": "537617ceb4001c52f4b9c3ea",
"stocks": [
{
"name": "dosh",
"_id": "5376022ba31c4b8ca8c45e30",
}
],
"createdDate": "2014-05-16T13:51:10.324Z"
}
]
如您所见,数组中的对象是Advertisment,它是广告模型。它有库存阵列。股票阵列中的股票是股票模型,这些股票已经存在于核心数据中。现在我将广告保存到核心数据,但它有这些股票对象,所以我创建了它们之间的关系。
广告模型:
@class Stock;
@interface Advertisement : NSManagedObject
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * advertisementId;
@property (nonatomic, retain) NSSet *stocks;
@end
@interface Advertisement (CoreDataGeneratedAccessors)
- (void)addStocksObject:(Stock *)value;
- (void)removeStocksObject:(Stock *)value;
- (void)addStocks:(NSSet *)values;
- (void)removeStocks:(NSSet *)values;
@end
股票对象已经保存到核心数据中,它存在于核心数据中。我试图只通过stockId找到合适的股票,然后将其添加到广告的股票对象。我在下面使用它来保存数据。
[[AFHttpClient sharedClient] GET:@"/advertisments" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
if (response.statusCode == 200) {
NSArray *results = (NSArray *)responseObject;
if ([results count] > 0) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dict in results) {
Advertisement *advertisment = [Advertisement MR_createInContext:localContext];
advertisment.title = dict[@"title"];
advertisment.advertisementId = dict[@"_id"];
NSArray *stocks = dict[@"stocks"];
for (NSDictionary *stock in stocks) {
NSString *stockId = stock[@"_id"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"stockId == %@", stockId];
Stock *stock = [[Stock MR_findAllWithPredicate:predicate inContext:localContext] objectAtIndex:0]; //here using "_id" to find the according Stock
[advertisment addStocksObject:stock]; //and then add it to advertisment's stocks object
}
}
} completion:^(BOOL success, NSError *error) {
[self performFetch];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
} else {
}
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
但是当我打开CoreDataPro或任何sqlite应用程序来浏览核心数据数据时。我看到只有最后一个广告对象将股票保存到广告的股票对象中。怎么了?
添加MagicalRecord保存控制台日志:
2014-05-18 16:43:18.114 Admin[1509:600b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8fa2070) → Saving <NSManagedObjectContext (0x8fa2070): *** UNNAMED ***> on *** BACKGROUND THREAD ***
2014-05-18 16:43:18.115 Admin[1509:600b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8fa2070) → Save Parents? 1
2014-05-18 16:43:18.115 Admin[1509:600b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8fa2070) → Save Synchronously? 0
2014-05-18 16:43:18.115 Admin[1509:600b] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8fa2070) Context UNNAMED is about to save. Obtaining permanent IDs for new 3 inserted objects
2014-05-18 16:43:18.116 Admin[1509:600b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8f834e0) → Saving <NSManagedObjectContext (0x8f834e0): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***
2014-05-18 16:43:18.116 Admin[1509:600b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8f834e0) → Save Parents? 1
2014-05-18 16:43:18.116 Admin[1509:600b] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8f834e0) → Save Synchronously? 0
2014-05-18 16:43:18.117 Admin[1509:530f] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8f834e0) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 3 inserted objects
2014-05-18 16:43:18.131 Admin[1509:530f] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x8f834e0) → Finished saving: <NSManagedObjectContext (0x8f834e0): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***