NSFetchRequest不返回任何内容 [英] NSFetchRequest returning nothing

查看:235
本文介绍了NSFetchRequest不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以发现为什么这不返回任何ManagedObjects?我试图添加到ATNSManagedObject + EasyFetching类以下,但fetch结果不返回任何内容。如果我在EasyFetch类之外获取这些对象,我有100多个对象,所以我知道它不是CoreData为空。

  (void)deleteAllObjectsInContext; 
{
NSManagedObjectContext * context = [NSManagedObjectContext defaultContext];
NSEntityDescription * entity = [self entityDescriptionInContext:context];
NSFetchRequest * request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entity];
// [request setIncludesPropertyValues:NO];

NSError * error = nil;
NSArray * results = [context executeFetchRequest:request error:& error];
if(error!= nil)
{
//句柄错误
NSLog(@delete error);
}

for(NSManagedObject * thing in results){< --- | results shows 0 objects |
[context deleteObject:thing];
}

NSError * saveError = nil;
[context save:& saveError];
}


解决方案

尝试简化: / p>

  //首先获取上下文或将其作为参数传递(这通常是我为
//做的)一个deleteAll类级别的方法像这样,但你的调用

+(void)deleteAllObjectsInContext:(NSManagedObjectContext *)context {

NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@ yourEntity>];
//无谓词
//无sortDescriptors
$ b NS错误= error nil;
NSArray * results = [context executeFetchRequest:request error: & error];

if(!results || error){// nil是一个错误
//句柄错误
}

/ /使用结果做某事

}



只是想添加此

< /developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.htmlrel =nofollow>段落



返回值



符合从接收方和与接收方的持久存储协调器相关联的持久存储中提取的请求指定的条件的对象数组。如果发生错误,返回nil。如果没有对象符合请求指定的条件,则返回一个空数组


Can anyone spot why this isn't returning any ManagedObjects? I'm trying to add to the ATNSManagedObject+EasyFetching class the following, but the fetch result returns nothing. If I fetch these outside of the EasyFetch class I have 100+ objects so I know it isn't CoreData being empty.

+ (void)deleteAllObjectsInContext;
{
    NSManagedObjectContext *context = [NSManagedObjectContext defaultContext];
    NSEntityDescription *entity = [self entityDescriptionInContext:context];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:entity];
    //[request setIncludesPropertyValues:NO];

    NSError *error = nil;
    NSArray *results = [context executeFetchRequest:request error:&error];
    if (error != nil)
    {
        //handle errors
        NSLog(@"delete error");
    }

    for (NSManagedObject *thing in results) {  <--- |results shows 0 objects|
        [context deleteObject:thing];
    }

    NSError *saveError = nil;
    [context save:&saveError];
}

解决方案

Try simplifying it to:

// first get the context or pass it in as an argument (this is usually what I do for
// a deleteAll class level method like this but your call

+ (void)deleteAllObjectsInContext:(NSManagedObjectContext*)context {

    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@ "<yourEntity>"];
    // no predicate
    // no sortDescriptors

    NSError* error = nil;
    NSArray* results = [context executeFetchRequest:request error:&error];

    if (!results || error) { // nil is an error
        // handle error
    }

    // do something with results

}

this way you can avoid having to retrieve an NSEntityDescription object.

UPDATE:

Just wanted to add this passage:

Return Value

"An array of objects that meet the criteria specified by request fetched from the receiver and from the persistent stores associated with the receiver’s persistent store coordinator. If an error occurs, returns nil. If no objects match the criteria specified by request, returns an empty array".

这篇关于NSFetchRequest不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆