gpt4 book ai didi

ios - CoreData 一次又一次地向数据库中添加数据

转载 作者:行者123 更新时间:2023-11-29 02:09:31 26 4
gpt4 key购买 nike

我已经在 APPDelegate 中实现了核心数据内容并创建了一个新类并从 JSON 和 NSURLConnection 获取数据 didfinishloading 我正在使用快速枚举将数据存储到核心数据中它工作良好并在 tableview 中显示 nsfetch 请求但问题每次我启动应用程序时,它都会一次又一次地将数据存储到 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 - CoreData 一次又一次地向数据库中添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29432950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com