将获取的NSManagedObject分配给属性

时间:2013-08-18 18:21:55

标签: core-data nsmanagedobject nsfetchrequest stackmob

这个问题可能看起来很长,但我确信核心数据专家的相对性很简单。显示配置使这个问题很长。谢谢!

在CoreData中,我有一个User实体和一个从该实体创建的NSManagedObject子类(User.h/.m)。不确定它是否相关,但我正在使用Stackmob远程数据库。

以下是我的抓取请求在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设置: enter image description here

为什么这不起作用?用户仍然显示为故障?我在获取请求中有以下行:[fetchRequest setReturnsObjectsAsFaults:NO];

2 个答案:

答案 0 :(得分:1)

在日志中显示的托管对象是错误的。使用它或访问其属性,Core Data将以最有效的方式填充错误,同时减少资源压力

这最后一部分很重要,因为这是您不想真正改变Core Data的错误行为的原因。发送通知的必要性不会改变它。

另外,请参考Core Data Programming Guide中的错误部分:

  

批次错误

     

您可以通过使用带有IN运算符的谓词执行获取请求来批量处理对象集合,如以下示例所示。 [...]

NSArray *array = [NSArray arrayWithObjects:fault1, fault2, ..., nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN %@", array];
  

在OS X v10.5及更高版本中,在创建提取请求时,可以使用NSFetchRequest方法setReturnsObjectsAsFaults:来确保托管对象不会作为错误返回。

因此,您可以看到,特殊的获取请求快捷方式仅适用于OS X.请在上面的源代码中阅读以查看备选方案预取

答案 1 :(得分:1)

这是StackMob限制。在https://developer.stackmob.com/ios-sdk/core-data-guide 明确声明returnObjectsAsFaults / setReturnObjectsAsFaults: (尚未)支持。