没有托管对象上下文时无法执行操作

时间:2015-09-22 19:12:33

标签: ios objective-c core-data nspredicate

我正在尝试将核心数据查询中的项目添加到nsmutablearray但我收到此错误:

"Cannot perform operation without a managed object context"

这是我的代码:

NSPredicate *productPredicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
NSManagedObjectContext *moc = self.managedObjectContext;
NSEntityDescription *description = [ NSEntityDescription entityForName:@"Product" inManagedObjectContext:moc];
NSFetchRequest *request = [NSFetchRequest new];
request.predicate = productPredicate;
request.entity = description;
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];


for (NSInteger i = 0; i < results.count; i++)
{

    NSString *productName = [[results objectAtIndex:i] valueForKey:@"productName"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productName == %@", productName];
    NSArray *match = [_listOfProducts filteredArrayUsingPredicate:predicate];
    if (match == 0)
    {
        [self.listOfProducts addObject:[results objectAtIndex:i]];
    }
}

我收到错误的地方是将对象添加到nsmutablearray的时刻:

[self.listOfProducts addObject:[results objectAtIndex:i]];

你们中的任何人都知道我为什么会收到这个错误?我非常感谢你的帮助。

0 个答案:

没有答案