2012-12-13 32 views
0

我想通過它的唯一對象獲取對象,爲此我保存了插入對象的id,我將用它來取回它,這裏是我的代碼。作爲屬性時使用NSManagedObjectID問題

@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) { 
     [email protected]"1001"; 
     dept.name=department; 
     [email protected]"ahmedabad"; 
     [email protected]"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;  
} 

那麼這裏有什麼問題?雖然我已經存儲爲屬性,但我無法在第二個函數中訪問moid

回答

1

首先,你是否在使用ARC?

那麼,爲什麼呢?

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

    return @"true" 
} 

使用這個代替

- (BOOL)insertUser 
{ 
    // other code here 

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

然後,使用此方法

- (NSArray *)getUser 

- (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]; 
} 

關於你的問題,你不應該依賴NSManagedObjectID

正如@ MarcusS.Zarra建議

的NSManagedObjectID不能保證是一致的。它可以根據包括數據遷移和其他因素在內的多種因素進行更改。如果您將此用作您的 對象的唯一標識符,請停止。

所以,在模型中添加屬性(一NSString可能是OK)爲UserInfo實體,並設置它,當你創建一個新用戶,並在獲取用戶。

// insert 
user.userIdentifier = // your identifier 

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

現在在哪裏moid will be

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

將被設置爲

self.moid = [newUser userIdentifier]; 

要創建一個新的標識,你可以在CORE DATA objectId changes constantly看看,或者創建自己的算法生成它。

P.S.我認爲你沒有使用ARC,因爲編譯器會抱怨newUser

+0

是的,我正在ARC支持的環境中工作,我正在嘗試保存唯一的ID,後來爲了更新目的使用該ID獲取對象。 – BaSha

0

有兩種形式的對象ID。首次創建託管對象時,核心數據會爲其分配一個臨時ID;只有在保存到持久性存儲中時,Core Data纔會爲託管對象分配一個永久ID。您可以隨時發現某個ID是否是暫時的:

BOOL isTemporary = [[managedObject objectID] isTemporaryID]; 

保存上下文後,您可能會更改對象的id。

+0

我保存newUser後,檢查這是給NULL! BOOL isTemporary = [[newUser objectID] isTemporaryID]; NSLog(@「IS TEMP:%@」,isTemporary); – BaSha

+0

實際上%@用於參考打印對象。所以NSLog(@「IS TEMP:%@」,isTemporary);是錯的。因爲No返回0並且它在地址處找到Null)。 –

+0

這意味着你的isTemprory正在返回NO –