Frage

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?

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top