0

I'm completely new to coredata few days back i started using core data. i have written a predicate to fetch data from coredata if i log data in current method it works fine. if i log data in another method it shows "data: "

<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
4
  • possible duplicate of Coredata Error "data: <fault>" Commented May 13, 2015 at 13:31
  • @Azat but that answer didn't work for me though i user [request setReturnsObjectsAsFaults:NO] Commented May 13, 2015 at 13:33
  • @Azat What the question started asking was a duplicate of that, but since core data faults are not a problem, and this code has some sort of problem in it, as long as it is edited to reflect that, it will no longer be a duplicate. Commented May 13, 2015 at 13:37
  • @WillM. however I cannot cancel my flag Commented May 13, 2015 at 13:39

2 Answers 2

1

The problem is that both storage and moc are created locally in dataForJid:. When that method completes, both these variables will be deallocated. Hence the NSManagedObjects become invalid. You need to keep a strong reference to your CoreData stack - for example by making MKAUserProfileCoreData into a singleton, or by building the stack directly in your view controller.

Sign up to request clarification or add additional context in comments.

Comments

0

Does your code work fine or have any issues running? Core data faults are not bad, they are a way of saving memory. A core data fault means that the entire object has not yet been loaded into memory. However, as soon as the object is requested, it will be loaded into memory.

2 Comments

If i request object it is returning null
@user3252627 please edit your question to include your actual problem and more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.