我正在尝试通过其唯一对象来获取对象,为此我正在保存插入对象的 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
尽管我已存储为属性,但我无法访问我的第二个功能。