【发布时间】:2015-04-03 13:16:22
【问题描述】:
我已经在 APPDelegate 中实现了核心数据的东西,并创建了一个新类并从 JSON 和 NSURLConnection 获取数据 didfinishloading 我正在使用快速枚举将数据存储到核心数据中,它运行良好并使用 nsfetch 请求在 tableview 中显示,但是问题每次我启动应用程序时,它都会一次又一次地将数据存储到 sqlite 文件中,为什么会发生这种情况,方法如下
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.customCellClass = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (self.customCellClass == nil)
{
self.customCellClass = [[CellCustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
self.customCellClass.nameLabel.text = [_myArray[indexPath.row] name];// label
self.customCellClass.cityLabel.text = [_myArray[indexPath.row] regions]; // label
self.customCellClass.detailLabel.text = [_myArray[indexPath.row] summary]; //label
return self.customCellClass;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
arrayData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
appDelegate = [[UIApplication sharedApplication ]delegate];
context = [appDelegate managedObjectContext];
NSString * name ;
NSString * summary;
NSString * region;
for (NSDictionary * dic in arrayData) {
name = [dic objectForKey:@"name"];
summary = [dic objectForKey:@"summary"];
region = [dic objectForKey:@"region"];
Discount * d = [NSEntityDescription insertNewObjectForEntityForName:@"Discount" inManagedObjectContext:context];
d.name = name;
d.summary = summary;
d.regions = region;
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Getting error while saving data");
}
else{
NSLog(@"Saved");
}
[listTableView reloadData];
}
appdelegate 方法很常见,因为每个人都提供 请有人给我解决方案
【问题讨论】:
标签: ios objective-c iphone core-data