这个问题可能看起来很长,但我相信对于核心数据专家来说它是相对简单的。显示配置使这个问题变得很长。谢谢!
在 CoreData 中,我有一个User
实体和一个从该实体创建的NSManagedObject
子类(User.h/.m)
。不确定它是否相关,但我正在使用带有 Stackmob 的远程数据库。
这是我的 fetch 请求在 Review.m 中的样子:
获取请求:
-(NSArray *)fetchRequest
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//Please Note the line below:
[fetchRequest setReturnsObjectsAsFaults:NO];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username == %@", self.usernameField.text];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
if (fetchedObjects.count>=1)
{
NSLog(@"Number of Objects Returned %i", fetchedObjects.count);
NSManagedObject *fetchedObject = [fetchedObjects objectAtIndex:0];
}
这是日志:
2013-08-18 10:26:25.082 Time[995:c07] Number of Objects Returned 1
2013-08-18 10:26:25.082 Time[995:c07] User Object: <User: 0xe07b260> (entity: User; id: 0xa65ab70 <x-coredata://392983AD-D649-4D68-A93F-D86109BC009C-995-000004B584F1BB06/User/piphone5> ; data: <fault>)
几周前,我使用以下内容成功创建了用户对象:
User *newUser = [[User alloc] initIntoManagedObjectContext:self.managedObjectContext];
[newUser setValue:self.usernameField.text forKey:[newUser primaryKeyField]];
[newUser setValue:self.emailAddressField.text forKey:@"email"];
[newUser setPassword:self.passwordField.text];
[newUser setValue:self.gender forKey:@"gender"];
[self.managedObjectContext saveOnSuccess:^{
[self.client loginWithUsername:self.usernameField.text password:self.passwordField.text onSuccess:^(NSDictionary *results) {
NSLog(@"Login Success %@",results);
} onFailure:^(NSError *error) {
NSLog(@"Login Fail: %@",error);
}];
这就是我想在 Review.m 中做的事情:
@interface Review()
@property (strong, nonatomic) User *user;
@end
@sythesize = user;
....
if (fetchedObjects.count>=1)
{
NSLog(@"Number of Objects Returned %i", fetchedObjects.count);
NSManagedObject *fetchedObject = [fetchedObjects objectAtIndex:0];
NSLog(@"fetchObject Object %@", fetchedObject);
//Addition
user = (User*)(fetchedObject);
NSLog(@"User %@", user);
}
这是日志:
2013-08-18 11:07:13.313 Time[1177:c07] Number of Objects Returned 1
2013-08-18 11:07:13.314 Time[1177:c07] fetchObject Object <User: 0xa532cc0> (entity: User; id: 0xb426de0 <x-coredata://3336122B-7117-4D92-B0A1-DDBAF80DDBF7-1177-000006EEE046E326/User/piphone5> ; data: <fault>)
2013-08-18 11:07:13.314 Time[1177:c07] User <User: 0xa532cc0> (entity: User; id: 0xb426de0 <x-coredata://3336122B-7117-4D92-B0A1-DDBAF80DDBF7-1177-000006EEE046E326/User/piphone5> ; data: <fault>)
为什么这不起作用?用户仍然显示为故障?我在获取请求中有以下行:[fetchRequest setReturnsObjectsAsFaults:NO];
以下是我需要这样做的原因:
Notification *notificationObject = [NSEntityDescription insertNewObjectForEntityForName:@"Notification" inManagedObjectContext:self.managedObjectContext];
[notificationObject setValue:[NSNumber numberWithInt:1] forKey:@"appType"];
[notificationObject setValue:[notificationObject assignObjectId] forKey:[notificationObject primaryKeyField]];
NSError *error = nil;
if (![self.managedObjectContext saveAndWait:&error])
{
NSLog(@"There was an error");
}
else
{
NSLog(@"Notification Object Created");
}
//I need to Add the fetchedObject as UsersObject (which is a relationship)
[notificationObject addUsersObject:user];
[self.managedObjectContext saveOnSuccess:^{ ....
这是我的 CoreData 设置:
为什么这不起作用?用户仍然显示为故障?我在获取请求中有以下行:[fetchRequest setReturnsObjectsAsFaults:NO];