我已經在APPDelegate中實現了核心數據的東西,並創建了一個新類,並從JSON和NSURLConnection獲取數據didfinishloading我正在使用快速枚舉將數據存儲到核心數據,它工作良好並顯示與nsfetch請求,但問題的tableview是我每次啓動它一次又一次地將數據存儲到sqlite的文件爲什麼會發生的方法的應用都低於CoreData一次又一次地將數據添加到數據庫
-(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方法只是時間常見於每一個給出 有一個給我的解決方案請
註釋不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/74574/discussion-on-answer-by-vladz-coredata-is-adding-data-again-and-again-in-到的數據)。 – Taryn 2015-04-06 15:01:38