我正在使用核心数据制作应用程序。
它是 facebook 应用程序,所以我将用户信息保存到 core-data。
我想将个人资料图片添加到用户信息数据中。
for (UserInfo *info in fetchedObjects) {
if ([info.fbId isEqualToString:delegate.currentUserInfo.fbId]) {
info.details.photos.image = [NSData dataWithContentsOfURL:url];
info.details.photos.imageId = [result objectForKey:@"id"];
info.details.photos.details = info.details;
delegate.currentUserInfo = info;
for (UserInfo *info in fetchedObjects) {
NSLog(@"currentUserInfo : %@", info);
NSLog(@"curretnUserDetails : %@", info.details);
NSLog(@"currentUserPhoto : %@", info.details.photos);
}
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
}
像这样,我将数据添加到与FB用户ID对应的core-data(relationshop命名照片)中。
结果,中间的NSLog很好地显示了photoInfo数据,但是当我重新启动我的应用程序时,没有照片数据。
我好几天没解决这个问题。帮我!
-编辑
DataModel 的组织方式如下图所示。
我只使用一种上下文,而且我是新手,所以我无法理解所有感谢评论。
-编辑2
if (!beExist) {
UserInfo *userInfo = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:context];
userInfo.name = [myInfoDic objectForKey:@"name"];
userInfo.fbId = [myInfoDic objectForKey:@"id"];
userInfo.type = @"user";
UserDetails *userDetails = [NSEntityDescription insertNewObjectForEntityForName:@"UserDetails" inManagedObjectContext:context];
userDetails.gender = [myInfoDic objectForKey:@"gender"];
userDetails.work = [[[[myInfoDic objectForKey:@"work"] objectAtIndex:0] objectForKey:@"employer"] objectForKey:@"name"];
NSArray *eduArray = [myInfoDic objectForKey:@"education"];
userDetails.education = [[[eduArray objectAtIndex:eduArray.count-1] objectForKey:@"school"] objectForKey:@"name"];
userDetails.birthday = [myInfoDic objectForKey:@"birthday"];
userDetails.location = [[myInfoDic objectForKey:@"location"] objectForKey:@"name"];
PhotoInfo *photoInfo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoInfo" inManagedObjectContext:context];
userDetails.info = userInfo;
userInfo.details = userDetails;
userInfo.details.photos = photoInfo;
photoInfo.details = userDetails;
delegate.currentUserInfo = userInfo;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
我保存了这样的用户信息。我在 AppDelegate 中创建了上下文。我为 Core data 制作了项目,所以 Xcode 制作了那个代码。
我可以看到这个日志
2012-05-04 00:40:58.997 Feek[16423:fb03] currentUserPhoto : <PhotoInfo: 0x6d15d60> (entity: PhotoInfo; id: 0x6d18550 <x-coredata://2ED200FE-D799-4C66-A0AC-5BE1BC426439/PhotoInfo/p1> ; data: {
details = "0x8830ca0 <x-coredata://2ED200FE-D799-4C66-A0AC-5BE1BC426439/UserDetails/p1>";
image = <ffd8ffe0 00104a46 49460001 02000001 00010000 fffe0004 2a00ffe2 021c4943 435f5052 4f46494c 45000101 0000020c 6c63>;
imageId = 334168116641714;
})
我认为这是核心数据接受数据,但在杀死我的应用程序后它不会保留。