Populating an Array from Core Data in Xcode

 ̄綄美尐妖づ 提交于 2019-12-12 04:45:53

问题


I am trying to populate an NSArray with a collection of data I get from CoreData. But my array doesnt seem to be populating with the data. I have the following code to retrieve the data:

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]
                                initWithEntityName:@"WeightLog"];
self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest
                                                         error:nil] mutableCopy];

And I am using the following for loop to populate the NSArray with the data I collect from WeightLog for a particular field.

for (int i =0; i<=self.contactarray.count; i++) {
    NSManagedObject *device = [self.contactarray objectAtIndex:i];
    [titleNames addObject:device];
}

Just so you know contactarray is a property in my .h file of the following format:

@property (strong) NSMutableArray *contactarray;

Can you tell me where I am going wrong please, I am fairly new to iOS Development, if it doesn't show.

Thank you in advance


回答1:


Initialise titleNames array before use. Try this,

titleNames = [[NSMutableArray alloc] init];
for (int i =0; i<=self.contactarray.count; i++) {
    NSManagedObject *device = [self.contactarray objectAtIndex:i];
    [titleNames addObject:device];
}



回答2:


Just call this user-defined method. for ex -

self.titleNames = [self selectAllRowInEntity:@"WeightLog"];


-(NSArray *) selectAllRowInEntity:(NSString *) entityName
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fRequest;
    NSEntityDescription *eDesc;
    NSArray *arr;
    fRequest = [[NSFetchRequest alloc] init];
    eDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
    [fRequest setEntity:eDesc];


    arr =   [managedObjectContext executeFetchRequest:fRequest error:nil];
    return arr;

}



回答3:


This line here:

self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

This is a cardinal sin in Core Data - to not use the provided error parameters.

NSError *error;
self.contactarray = [[managedObjectContext executeFetchRequest:fetchRequest
                                                     &error] mutableCopy];

if (!self.contactArray) {
    // Fetch Requests return a nil on error, in which case you should check the error.
    NSLog(@"Error occurred: %@", error);
} else {
    // do whatever you want with the array
}

Now run your code and look at the console and you might see the reason for the error.

Edited to add

Following a comment:

You should always check that the return of the method is nil before evaluating the error object. For Cocoa (and Cocoa-Touch) methods this is the only time that the error parameter is guaranteed to be valid.

This is taken from the Error Handling Programming Guide



来源:https://stackoverflow.com/questions/22478350/populating-an-array-from-core-data-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!