0

私はその一意のオブジェクトによってオブジェクトをフェッチしようとしています。そのために、オブジェクトをフェッチするために使用する挿入オブジェクトのIDを保存しています。これが私のコードです。

@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;    
}

では、ここで何が問題になっていますか?moidプロパティとして保存しましたが、2番目の関数でアクセスできません。

4

2 に答える 2

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にエンティティの属性を追加し(aは問題ない可能性があります)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の絶え間ない変化を確認するか、それを生成するための独自のアルゴリズムを作成します。

PSコンパイラが。について文句を言うので、ARCを使用していないと思いますnewUser

于 2012-12-13T11:15:55.390 に答える
0

オブジェクトIDには2つの形式があります。管理対象オブジェクトが最初に作成されるときに、CoreDataはそれに一時IDを割り当てます。永続ストアに保存されている場合にのみ、CoreDataは管理対象オブジェクトに永続IDを割り当てます。IDが一時的なものであるかどうかを簡単に見つけることができます。

BOOL isTemporary = [[managedObject objectID] isTemporaryID];

コンテキストを保存した後、オブジェクトのIDが変更されている可能性があります。

于 2012-12-13T10:53:20.743 に答える