我正在构建一个从网址中提取JSON
并使用Core Data
和Magical Record
将其存储在设备上的应用程序。正在创建数据库,但关系在数据库中显示为null。我试图让它流动的方式是:
下面是我的数据模型,这一部分的一切似乎都很好。
这就是我解析数据并将其存储在实体
中的方法[_operationManager POST:@"feeds" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
id connections = [[operation.responseObject objectForKey:@"Data"]objectForKey:@"connections"];
username.sessionKey =[[operation.responseObject objectForKey:@"Data"]objectForKey:@"sessionId"];
id connection;
for(connection in connections){
//Get the JSON values
NSString *contentSystemID = [connection objectForKey:@"contentSystemId"];
NSString *contentSystemName = [connection objectForKey:@"name"];
NSString *logoUrl = [connection objectForKey:@"logo"];
NSNumber *unreadCount = [connection objectForKey:@"unread_count"];
NSLog(@"Content System ID = %@",contentSystemID);
NSLog(@"Content System Name = %@",contentSystemName);
NSLog(@"Logo URL = %@",logoUrl);
NSLog(@"Unread Count = %@",unreadCount);
FCConnection *connectionEntity = [FCConnection MR_createEntity];
connectionEntity.contentSystemID = contentSystemID;
connectionEntity.contentSystemName = contentSystemName;
connectionEntity.logoUrl = logoUrl;
connectionEntity.unreadCount = unreadCount;
id categories = [connection objectForKey:@"categories"];
for (id cat in categories)
{
NSString *title = [cat valueForKey:@"name"];
NSString *unreadCount = [cat valueForKey:@"unread_count"];
NSLog(@"title = %@",title);
NSLog(@"unread_count = %@",unreadCount);
id items = [cat objectForKey:@"items"];
// NSLog(@"items = %@",items);
for (id item in items){
//
// NSLog(@"items = %@",item);
NSNumber *isRead = [item valueForKey:@"unread"];
NSString *title = [item valueForKey:@"title"];
NSString *link = [item valueForKey:@"link"];
NSString *systemID = [item valueForKey:@"itemId"];
NSLog(@"isRead = %@",isRead);
NSLog(@"title = %@",title);
NSLog(@"link = %@",link);
NSLog(@"systemID = %@",systemID);
BaseTopic *baseEntity = [BaseTopic MR_createEntity];
baseEntity.title = title;
Article *article = [Article MR_createEntity];
article.link = link;
article.systemID = systemID;
article.isRead = isRead;
}
}
}
[MagicalRecord saveUsingCurrentThreadContextWithBlock:nil completion:^(BOOL success, NSError *error) {
NSLog(@"Data Stored");
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Failure
NSLog(@"Failed to fetch!");
} ];
}
我知道本节中的某些内容导致我的Core Data
文件如下所示。如您所见,数据正在分离并具有多行。应该只有三行,我猜是因为标题是在父实体“BasicTopic”中发生了这个问题。
我的下一个问题是关系没有在表格中填充。以下是FCConnection中与用户有关系的行,由于某些奇怪的原因,该行没有填充用户的pk。
任何帮助或建议将不胜感激。
答案 0 :(得分:0)
我没有看到您在数据导入代码中实际进行对象连接的位置。你应该有像
这样的东西[user addConnectionObject:connection]
在每个循环结束时,以及您需要创建的每个关系
答案 1 :(得分:0)
您需要将实体记录映射到每个关系。如果为这些实体生成托管对象子类,则可以看到每种关系的几种方法。你需要使用它来映射实体记录。
id connection;
for(connection in connections)
{
//Get the JSON values
NSString *contentSystemID = [connection objectForKey:@"contentSystemId"];
NSString *contentSystemName = [connection objectForKey:@"name"];
NSString *logoUrl = [connection objectForKey:@"logo"];
NSNumber *unreadCount = [connection objectForKey:@"unread_count"];
FCConnection *connectionEntity = [FCConnection MR_createEntity];
connectionEntity.contentSystemID = contentSystemID;
connectionEntity.contentSystemName = contentSystemName;
connectionEntity.logoUrl = logoUrl;
connectionEntity.unreadCount = unreadCount;
id categories = [connection objectForKey:@"categories"];
NSMutableArray *categoryArray = [NSMutableArray array];
for (id cat in categories)
{
NSString *title = [cat valueForKey:@"name"];
NSString *unreadCount = [cat valueForKey:@"unread_count"];
id items = [cat objectForKey:@"items"];
BaseTopic *baseEntity = [BaseTopic MR_createEntity];
baseEntity.title = title;
FCCatgory *category = [FCCatgory MR_createEntity];
NSMutableArray *itemArray = [NSMutableArray array];
for (id item in items)
{
NSNumber *isRead = [item valueForKey:@"unread"];
NSString *title = [item valueForKey:@"title"];
NSString *link = [item valueForKey:@"link"];
NSString *systemID = [item valueForKey:@"itemId"];
Article *article = [Article MR_createEntity];
article.link = link;
article.systemID = systemID;
article.isRead = isRead;
article.category = category;
[itemArray addObject:article];
}
[category addArticles:[NSSet setWithArray:itemArray]]; // mapping the items to category
category.connection = connectionEntity;
[categoryArray addObject:category];
}
[connectionEntity addCategories:[NSSet setWithArray:categoryArray]]; // mapping the items to connection entity
}
希望这会对你有所帮助