我正在尝试通过其唯一对象获取对象,为此我保存了插入对象的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
。
答案 0 :(得分: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
实体的模型中添加一个属性(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
。
答案 1 :(得分:0)
对象ID有两种形式。首次创建托管对象时,Core Data会为其分配一个临时ID;只有将其保存到持久性存储时,Core Data才会为托管对象分配永久ID。您可以轻松发现ID是否是临时的:
BOOL isTemporary = [[managedObject objectID] isTemporaryID];
保存上下文后,对象的id可能会发生变化。