我正在使用核心数据制作应用。
这是Facebook应用程序,所以我将用户信息保存到核心数据
我想将个人资料图片添加到用户信息数据中。
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对应的核心数据(称为照片的商店)。
因此,中间的NSLog显示了良好的photoInfo数据,但是当我重新启动应用程序时,没有照片数据。
我几天没有解决这个问题。救救我!
-edited
DataModel的组织方式与此图片类似。
我只使用一个上下文,我是新手所以我无法理解所有感谢的评论。
-edited2
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数据制作了项目,因此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;
})
我认为这是核心数据接受数据,但在杀死我的应用后它不会保留。