几天前我开始使用核心数据时,我对coredata完全陌生。我写了一个谓词来从coredata获取数据,如果我在当前方法中记录数据它工作正常。如果我用另一种方法记录数据,它会显示"数据:"
<Profile: 0x16451ba0> (entity: Profile; id: 0x1633c150 <x-coredata://41971DAD-4658-4C38-9D14-7FDFFA57E032/Profile/p6> ; data: <fault>)
-(void)populateCurrentUserData{
self.blockListArray = [self dataForJid:[[DataManager sharedHandler]userToken]];
Profile *profile = [self.blockListArray objectAtIndex:0];
NSLog(@"Data is :%@",profile.userId);//prints nil
NSLog(@"Data is :%@",self.blockListArray); //"<Profile: 0x17bb23f0> (entity: Profile; id: 0x17bb1fe0 <x-coredata://41971DAD-4658-4C38-9D14-7FDFFA57E032/Profile/p1> ; data: <fault>)"
}
-(NSArray *)dataForJid:(NSString *)inJid{
NSArray *data = [[NSArray alloc]init];
NSError *error;
self.dataArray = [[NSMutableArray alloc]init];
MKAUserProfileCoreData *storage = [[MKAUserProfileCoreData alloc]init];
NSManagedObjectContext *moc = [storage managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Profile" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSPredicate *profilePredicate = [NSPredicate predicateWithFormat:@"userId = %@", inJid];
[request setPredicate:profilePredicate];
[request setEntity:entity];
[request setReturnsObjectsAsFaults:NO];
data = [moc executeFetchRequest:request error:&error];
NSLog(@"Data is :%@",data); //This log works fine
return data;
}
/.h
#import <Foundation/Foundation.h>
@interface MKAUserProfileCoreData : NSObject
{
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
/.m
#import "MKAUserProfileCoreData.h"
#import <CoreData/CoreData.h>
@implementation MKAUserProfileCoreData
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"splashUserProfile.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error]) {
NSLog(@"Error is %@",error);
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should
not use this function in a shipping application, although it may be useful during
development. If it is not possible to recover from the error, display an alert panel that
instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object
model
Check the error message to determine what the actual problem was.
*/
abort();
}
return persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//NSLog(@"Path is %@", path);
return path;
}
@end
答案 0 :(得分:1)
问题是storage
和moc
都是dataForJid:
本地创建的。当该方法完成时,这两个变量都将被释放。因此NSManagedObjects变得无效。您需要对CoreData堆栈保持强引用 - 例如,将MKAUserProfileCoreData
放入单例中,或者直接在视图控制器中构建堆栈。
答案 1 :(得分:0)
您的代码是否正常运行或运行有问题?核心数据故障也不错,它们是一种节省内存的方法。核心数据错误意味着整个对象尚未加载到内存中。但是,只要请求对象,它就会被加载到内存中。