简体   繁体   English

我是否总是从CoreData获取NSArray?

[英]Do I always fetch into a NSArray from CoreData?

I am fetching something from CoreData where I expect 1 result or nil . 我从CoreData那里获取了一些1 result ,我期望1 resultnil

Currently I set the fetch into a NSArray and I attempted as it stand to fetch into a IconRoutine* object but the [context executeFetchRequest:fetchIcon error:&error]; 目前,我将获取设置为NSArray并尝试按原样获取到IconRoutine*对象,但是[context executeFetchRequest:fetchIcon error:&error]; needs to fetch into an array, thus resulting in a crash when I tried that. 需要读取到数组中,因此在尝试时会导致崩溃。

I guess what I wonder is if I can maybe in another way to fetch into a entity object so that I dont need the if ( [Icon count] !=0 ) to check for a nil and I can just return whatever is fetched and deal with the nil entity in another method. 我想我想知道的是我是否可以以另一种方式提取到entity object以使我不需要if ( [Icon count] !=0 )检查是否为nil而我可以只返回提取并处理的任何内容与nil entity在另一种方法中。

or maybe just a more efficient way (if there is one) to deal with results that you expect 1 or nil . 或者也许是一种更有效的方式(如果有)来处理您期望1nil

- (IconRoutine *) getIconRoutine {

    NSFetchRequest *fetchIcon = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityItem = [NSEntityDescription entityForName:@"IconRoutine" inManagedObjectContext:context];
    [fetchIcon setEntity:entityItem];

    [fetchIcon setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"User",@"Routine", nil]];

    [fetchIcon setPredicate:[NSPredicate predicateWithFormat:@"(routine.routineName == %@) AND (user.email LIKE %@) AND (author LIKE %@)",_routineNameInput.text,_appDelegate.currentUser,_routineAuthor]];

    NSError *error = nil;
    NSArray* Icon = [context executeFetchRequest:fetchIcon error:&error];

    if ( [Icon count] !=0 ) {
        return Icon[0];
    }
    return NO;    
}

Here is an option. 这是一个选项。 Not necessarily the solution you are looking for but one that may help: 不一定是您正在寻找的解决方案,但可能会有所帮助:

- (IconRoutine *) getIconRoutine {

    NSFetchRequest *fetchIcon = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityItem = [NSEntityDescription entityForName:@"IconRoutine" inManagedObjectContext:context];
    [fetchIcon setEntity:entityItem];

    [fetchIcon setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"User",@"Routine", nil]];

    [fetchIcon setPredicate:[NSPredicate predicateWithFormat:@"(routine.routineName == %@) AND (user.email LIKE %@) AND (author LIKE %@)",_routineNameInput.text,_appDelegate.currentUser,_routineAuthor]];

    return [context executeFetchRequest:fetchIcon error:nil].lastObject;    
}

This obviously only works if you don't care about the error message. 显然,这仅在您不关心错误消息时才有效。 lastObject will return nil if either the NSArray is nil or if the array is empty (so never an indexOutOfBoundsException )! 如果NSArray为nil或数组为空,则lastObject将返回nil(因此永远不会indexOutOfBoundsException )! Otherwise, it will return the last object, and if there is only one it will just return that. 否则,它将返回最后一个对象,如果只有一个,它将返回该对象。

If you do care about the error you could just simply do: 如果您确实关心该错误,则只需执行以下操作:

- (IconRoutine *) getIconRoutine {

    // fetch code from above
    // ..    

    NSError *fetchError
    IconRoutine *result = [context executeFetchRequest:fetchIcon error:&fetchError].lastObject;

    if (fetchError) {
        // handle the error
    }

    // return whatever result is anyway because if there was an error it would already be nil, and if not then it is the object you are looking for
    return result;
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM