Iphone 在核心数据中添加大量属性,从phone而不是Xcode启动时崩溃
我正在尝试向CoreData添加数据。当我从Xcode构建到手机时,它工作正常,但当我试图直接从iPhone启动应用程序时,它会在第一次保存到上下文时崩溃 我读了一个通过iTunes文件共享同步的文本文件,该文件相当大(约350000行)。我从文件中获得的值被添加到两个不同的数组(条形码和产品名称)。这些数组随后被批处理,并发送到我保存数据的函数 来自数组循环: [……] [……] 然后是保存功能:Iphone 在核心数据中添加大量属性,从phone而不是Xcode启动时崩溃,iphone,objective-c,core-data,Iphone,Objective C,Core Data,我正在尝试向CoreData添加数据。当我从Xcode构建到手机时,它工作正常,但当我试图直接从iPhone启动应用程序时,它会在第一次保存到上下文时崩溃 我读了一个通过iTunes文件共享同步的文本文件,该文件相当大(约350000行)。我从文件中获得的值被添加到两个不同的数组(条形码和产品名称)。这些数组随后被批处理,并发送到我保存数据的函数 来自数组循环: [……] [……] 然后是保存功能: -(void)addBatchOfData { NSAutoreleasePool *pool
-(void)addBatchOfData {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError *error;
NSUInteger loopLimit = 5000;
NSUInteger loopCounter = 0;
NSString *ean;
NSString *designation;
for (int i=0; i<[barcodes count];i++ ) {
ean = [barcodes objectAtIndex:i];
designation = [productNames objectAtIndex:i];
Product *product = (Product *)[NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:importContext];
[product setDesignation:designation];
[product setBarcode:ean];
loopCounter ++;
if (loopCounter == loopLimit) {
NSLog(@"Save CoreData");
[importContext save:&error];
[importContext reset];
[pool drain];
pool = [[NSAutoreleasePool alloc] init];
loopCounter = 0;
}
}
// Save any remaining records
if (loopCounter != 0) {
[importContext save:&error];
[importContext reset];
}
[pool drain];
-(void)addBatchOfData{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
n错误*错误;
NSU整数循环限制=5000;
整数循环计数器=0;
NSString*ean;
NSString*名称;
对于(inti=0;i,我建议您实现此委托方法,然后尝试查看内存发生了什么
在模拟器中运行时,您没有内存限制,但在手机中运行时会有内存限制
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
}
我想我找到了问题的答案
我所做的是,我在“-(void)viewdiload{”中启动了所有繁重的数据处理。当我在应用程序中单击一个按钮后将其更改为启动处理时,它工作得很好
现在只需要找出从哪里开始数据处理,有什么建议吗?在后台线程中卸载你的文件加载,让手机启动你的主窗口和视图。如果你不及时提供视图(这就是你看到的),iOS将杀死你的应用程序
对于xml->CoreData转换器代码,我必须这样做。我只是向用户展示一个视图,通知他们正在发生的事情和一个进度条(我使用)。
比如:
self.hud = [[MBProgressHUD alloc] initWithView:window];
// Set determinate mode
hud.mode = MBProgressHUDModeDeterminate;
hud.delegate = self;
hud.labelText = @"Converting Data File";
[self.window addSubview:hud];
// Show the HUD while the provided method executes in a new thread
[hud showWhileExecuting:@selector(convertToCoreDataStoreTask) onTarget:self withObject:nil animated:YES];
您只需确保在新线程中使用单独的NSManagedObjectContext即可。谢谢,问题是我没有在模拟器中运行它,而是将它构建到设备上。当我这样做时,它工作正常……谢谢Brent!这实际上就是我解决问题的方法。我惊讶于创建新线程有多么容易。
self.hud = [[MBProgressHUD alloc] initWithView:window];
// Set determinate mode
hud.mode = MBProgressHUDModeDeterminate;
hud.delegate = self;
hud.labelText = @"Converting Data File";
[self.window addSubview:hud];
// Show the HUD while the provided method executes in a new thread
[hud showWhileExecuting:@selector(convertToCoreDataStoreTask) onTarget:self withObject:nil animated:YES];