将Json数据保存到coredata IOS并同时在UITableView中显示

将Json数据保存到coredata IOS并同时在UITableView中显示,ios,xcode,core-data,Ios,Xcode,Core Data,我是IOS Xcode编程新手。目前我正在开发一款使用Json数据的应用程序。 应用程序读取的Json数据可能非常大。我需要解析数据并将其存储到核心数据中,这样当应用程序下次运行时,它就可以从那里简单地读取数据,从而节省大量时间。我曾尝试使用dispatch_async,但在保存数据时UI似乎被冻结,这也导致应用程序崩溃。 我使用ASIHTTPRequest读取和解析Json数据,虽然效果很好,但在这一部分,我必须将数据保存到核心数据,并同时将其加载到UITableView中,这被证明是一件痛苦

我是IOS Xcode编程新手。目前我正在开发一款使用Json数据的应用程序。 应用程序读取的Json数据可能非常大。我需要解析数据并将其存储到核心数据中,这样当应用程序下次运行时,它就可以从那里简单地读取数据,从而节省大量时间。我曾尝试使用dispatch_async,但在保存数据时UI似乎被冻结,这也导致应用程序崩溃。 我使用ASIHTTPRequest读取和解析Json数据,虽然效果很好,但在这一部分,我必须将数据保存到核心数据,并同时将其加载到UITableView中,这被证明是一件痛苦的事情。 如果有人能帮我,我将非常感激

这是我的密码

NSString *connectionString = [NSString stringWithFormat:@"%@%@?song_id=%@", SERVER_STRING, URL_GET_SONG_LIST, lasSongID];

NSLog(@"COnnection String is:\n%@", connectionString);
NSURL* url = [NSURL URLWithString:connectionString];

//The actual request
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

// Becoming the request delegate
//To get callbacks like requestFinished: or requestFailed:
[request setDelegate:self];
NSLog(@"Fetching Dataaaaaaaa from %@",url);

// Fire off the request
[request startAsynchronous];

-(void) requestFinished: (ASIHTTPRequest *) request 
{
    NSString *theJSON = [request responseString];
    NSLog(@"Dataaaaaaaa,%@",theJSON);
    NSDictionary *responseDictionary = [theJSON JSONValue];

    if ([[responseDictionary valueForKey:@"Message"] isKindOfClass:[NSArray class]])
    {
        [songsArray addObjectsFromArray:[responseDictionary valueForKey:@"Message"]];

        if (songsArray.count > 0) 
        {
            dispatch_async (bgQueue, ^(void){
                [self saveDownloadedSongs];            
            });
        }
    }
}
saveDownloadedSongs->在一些验证后将Json保存到我的核心数据中

为要存储的实体创建NSFetchedResultsController

@property (nonatomic) NSFetchedResultsController fetchedResultsController;
//Initialize it in your viewDidLoad
视图控制器应该是NSFetchedResultsControllerDelegate 为NSFetchedResultsController实现委托方法

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView reloadData];
}
实现UITableView的数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.fetchedResultsController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.fetchedResultsController.sections[0] numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    SomeObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.label.text = object.property
    return cell;
}
每次您持久化一个新对象时,您的委托都会自动触发并重新加载该表,该表现在包含新对象

编辑:

如果要节省时间,请创建一个新的主详细信息应用程序。在MasterViewController中,您将找到步骤1和步骤3中平滑动画的源代码