我正在尝试通过唯一的对象来获取对象,为此我保存了插入对象的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,尽管我已经存储为属性。
发布于 2012-12-13 19:15:55
首先,你是否在使用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是一致的。它可能会根据许多因素而变化,包括数据迁移和其他因素。如果将其用作对象的唯一标识符,请停止。
因此,在模型中为UserInfo实体添加一个属性( NSString就可以了),并在创建新用户和检索用户时设置它。
// 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或创建自己的算法来生成它。
附言:我认为你没有使用ARC,因为编译器会抱怨newUser。
发布于 2012-12-13 18:53:21
对象ID有两种形式。首次创建托管对象时,Core Data会为其分配临时ID;只有将其保存到永久存储区后,Core Data才会为托管对象分配永久ID。您可以很容易地发现ID是否是临时的:
BOOL isTemporary = [[managedObject objectID] isTemporaryID];保存上下文后,您的对象id可能会发生更改。
https://stackoverflow.com/questions/13857635
复制相似问题