2011-11-12 16 views
0

我正在创建一个使用核心数据的应用程序。我有一个用户在其中创建要在主视图中使用的记录的反面。我在MainViewController的ViewWillAppear中获取核心数据。当我在新设备上运行应用程序时出现问题,它返回数组中没有数据的错误。数组中没有数据

我填充数组如下:

/* 
Fetch existing events. 
Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch. 
*/ 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Child" inManagedObjectContext:_managedObjectContext]; 
[request setEntity:entity]; 

// Order the events by creation date, most recent first. 
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
NSSortDescriptor *prizeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"prize" ascending:NO]; 
NSSortDescriptor *neededDescriptor = [[NSSortDescriptor alloc] initWithKey:@"marblesneeded" ascending:NO]; 
NSSortDescriptor *colorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"color" ascending:NO]; 



NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:nameDescriptor,prizeDescriptor,neededDescriptor,colorDescriptor, nil]; 
[request setSortDescriptors:sortDescriptors]; 
[nameDescriptor release]; 
[colorDescriptor release]; 
[prizeDescriptor release]; 
[neededDescriptor release]; 
[sortDescriptors release]; 

// Execute the fetch -- create a copy of the result. 
NSError *error = nil; 
records = [[self.managedObjectContext executeFetchRequest:request error:&error] retain]; 

if ([records count] == 0) { 
    recordsempty = YES; 
} 

[request release]; 

而且我得到以下错误:

-[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array' 

没有人有帮助防止这种崩溃,当用户从安装我的应用程序的任何想法应用商店?

感谢

+1

如果您提供了有关崩溃的详细信息以及一些显示如何将对象放入数组的代码,将会有所帮助。 – timthetoolman

+0

@timthetoolman,我添加了代码和错误。 –

+0

你应该在代码中指出你得到错误的地方。如果只有在没有数据时才会出现错误,那么在某处您有一些代码需要索引为0的数据。 –

回答

0

后,我加入一个if语句不尝试,如果数组是空的,以显示任何问题得到了解决。然后,我使用UIAlertView将用户引导至反面。