0

I'm trying to fetch object by its unique object, and for that I'm saving id of inserting object which I will use to fetch it back, here is my code.

@implementation UserInfoDOA
@synthesize firstName,lastName,userName,bDate,department,searchByDept,searchByName,moid,uriData,uri;

- (NSString *)insertUser
{    
    UserInfoAppDelegate *delegate = (UserInfoAppDelegate *)[[UIApplication sharedApplication] delegate];
    UserInfo *newUser = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo"
                                  inManagedObjectContext:delegate.managedObjectContext];


    Department *dept = [NSEntityDescription insertNewObjectForEntityForName:@"Department"
                                                   inManagedObjectContext:delegate.managedObjectContext];

    moid = [newUser objectID];
    NSLog(@"IN INSERTUSER %@", moid);  // moid displays its value

    if(dept!=nil) {
        dept.id=@"1001";
        dept.name=department;
        dept.location=@"ahmedabad";
        dept.post=@"developer";        
    }

    if (newUser != nil) {
        newUser.firstName =firstName;
        newUser.lastName =lastName;
        newUser.userName=userName;
        newUser.bDate = bDate ;
        newUser.dept=dept;

        NSError *savingError = nil;

        if ([delegate.managedObjectContext save:&savingError]) {
            NSLog(@"Successfully saved the context.");
        }
        else {       
            NSLog(@"Failed to save the context. Error = %@", savingError);
        }
    }
    else {        
        NSLog(@"Failed to create the new person.");
    }

    NSLog(@"IN INSERTUSER after %@",moid);

    return @"true";
}

- (NSArray *) getUser
{
    NSLog(@"IN GETUSER %@", moid); //NOT WORKING, moid is NULL here

    UserInfoAppDelegate *delegate = (UserInfoAppDelegate *)[[UIApplication sharedApplication] delegate];


    NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo"
                                              inManagedObjectContext:delegate.managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entity];


    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"(objecId = %@)",moid];


    [fetchRequest setPredicate:predicate];

    NSError *requestError = nil;
    NSArray *users = [delegate.managedObjectContext executeFetchRequest:fetchRequest                     
                                                                  error:&requestError];

    return users;    
}

So what is wrong here? I cannot access moid in my second function though I have stored as property.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
BaSha
  • 2,356
  • 3
  • 21
  • 38

2 Answers2

1

First of all, are you using ARC or not?

Then, why this?

- (NSString *)insertUser
{
    // other code here

    return @"true"
}

use this instead

- (BOOL)insertUser
{
    // other code here

    return YES // or NO based on the insertion result
}

Then, use this method

- (NSArray *)getUser

with

- (UserInfo *)getUser
{
    // other code here

    // maybe you should add some code for error handling...you haven't set up any code there...
    return [users objectAtIndex:0];
}

About your question, you should not rely on NSManagedObjectID.

As @MarcusS.Zarra suggested

The NSManagedObjectID is not guaranteed to be consistent. It can change based on a number of factors including data migration and other factors. If you are using this as a unique identifier for your objects, stop.

So, add an attribute in your model (a NSString could be ok) for the UserInfo entity and set it when you create a new user and when you retrieve a user.

// insert
user.userIdentifier = // your identifier

// retrieve
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(userIdentifier == %@)", self.moid];

Where now moid will be

@property (nonatomic, copy) NSString* moid; // also synthesize it (optional)

that will be set as

self.moid = [newUser userIdentifier];

To create a new identifier you could take a look at CORE DATA objectId changes constantly or create your own algorithm for generating it.

P.S. I think you are not using ARC because the compiler would complain about newUser.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • yes i'm working on ARC enabled environment, i was trying to save unique id and later on fetch object with that id for update purpose. – BaSha Dec 13 '12 at 11:26
0

There are two forms of an object ID. When a managed object is first created, Core Data assigns it a temporary ID; only if it is saved to a persistent store does Core Data assign a managed object a permanent ID. You can readily discover whether an ID is temporary:

BOOL isTemporary = [[managedObject objectID] isTemporaryID];

It may possible that you id of object is getting changed after saving context.

Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
  • i checked this after saving newUser, which is giving NULL! BOOL isTemporary = [[newUser objectID] isTemporaryID]; NSLog(@"IS TEMP: %@",isTemporary); – BaSha Dec 13 '12 at 11:02
  • actually %@ is used for printing object at reference. so NSLog(@"IS TEMP: %@",isTemporary); is wrong. since No returns 0 and it finds Null at ) address. – Sunil Pandey Dec 13 '12 at 11:10
  • it means your isTemprory is returning NO – Sunil Pandey Dec 13 '12 at 11:10