1

I am using core data for my application.It works fine on simulator but not retreiving the details on real device.Device is of iOS6.1.This is the code i am using:

- (NSManagedObjectContext *) getCurrentMangedContext
{
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ForceData" withExtension:@"momd"];
    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];  

    NSURL *storeURL =  [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]URLByAppendingPathComponent:@"ForceData.sqlite"];

    NSError *error = nil;
    NSPersistentStoreCoordinator *persistantStroreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    if (![persistantStroreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {

    }
    NSManagedObjectContext *managedContext = [[NSManagedObjectContext alloc] init] ;
    [managedContext setPersistentStoreCoordinator:persistantStroreCoordinator];
    modelURL = nil;
    return  managedContext;
}

This is how i am saving my login details and it is not giving any error.

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
  [request setEntity:entity];
  NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    if (emailString != nil)
    {
       [newManagedObject setValue:emailString forKey:@"email"]; 
    }
    if (genderString != nil)
    {
        [newManagedObject setValue:genderString forKey:@"gender"]; 
    }
    if (fNameString != nil)
    {
        [newManagedObject setValue:fNameString forKey:@"firstName"]; 
    }
    if (lNameString != nil)
    {
        [newManagedObject setValue:lNameString forKey:@"lastName"]; 
    }
   if (userIDString != nil)
    {
        [newManagedObject setValue:userIDString forKey:@"userID"]; 
    }
  NSError *error = nil;
    if (![context save:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    // Insert User details to the user DB <--------------------------

And this is how i am retrieving:

- (User *) getActiveUser 
{
    NSManagedObjectContext *context = [self getCurrentMangedContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    [request setEntity:entity];

    NSError *errorFetch = nil;
    NSArray *array = [context executeFetchRequest:request error:&errorFetch];
    User *objUser = (User *) [array lastObject];
    NSLog(@"%@",objUser);

    return objUser;
}

But i am not getting the user details on device but getting on simulator.anyone faced this same?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
iOS Developer
  • 1,723
  • 2
  • 16
  • 47
  • 1
    Can you post the output of `NSLog(@"%@",objUser);`. BTW: Please don't prefix your getter methods with `get`. Simply `- (User *)activeUser` - by convention: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html – Florian Mielke Dec 14 '13 at 06:03
  • What debugging have you done? At least log the errors returned by each method... – Wain Dec 14 '13 at 08:42
  • @FlorianMielke thanks for the info.what i am getting in objUser is: (entity: User; id: 0x1facde20 ; data: ) 2013-12-14 18:31:31.811 ForcePack 1.3[3851:7607] (entity: User; id: 0x1faccb70 ; data: ) – iOS Developer Dec 14 '13 at 13:02
  • I'd suggest that the associated managed object context is no longer available. Can you please try to assign the managed object context to an appropriate property. A managed object can't live without it's managed object context being alive. – Florian Mielke Dec 14 '13 at 13:11

1 Answers1

0

In your case I'd suggest that ARC releases your managedObjectContext after executing the fetch request.

Make sure that you hold a strong reference to the appropriate managedObjectContext during the whole lifetime of your managedObject somewhere in your app (e.g. your ApplicationDelegate). A NSManagedObject can't live without it's managedObjectContext. The Core Data project template shows how to do that.

Further information about ARC and strong references: https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

Florian Mielke
  • 3,310
  • 27
  • 31