简体   繁体   中英

Do I always fetch into a NSArray from CoreData?

I am fetching something from CoreData where I expect 1 result or nil .

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]; 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.

or maybe just a more efficient way (if there is one) to deal with results that you expect 1 or nil .

- (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 )! 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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