1

IOS Xcode プログラミングは初めてです。現在、Json データを使用するアプリに取り組んでいます。アプリは、サイズが非常に大きい可能性がある Json データを読み取ります。データを解析して Core Data に保存する必要があります。これにより、次回アプリを実行するときにそこから簡単にデータを読み取ることができ、多くの時間を節約できます。使用してみ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 をコア データに保存します

4

1 に答える 1

0
  1. 保存するエンティティの NSFetchedResultsController を作成します

    @property (nonatomic) NSFetchedResultsController fetchedResultsController;
    //Initialize it in your viewDidLoad
    
  2. あなたのView ControllerはあなたのNSFetchedResultsControllerDelegateでなければなりません
  3. NSFetchedResultsController のデリゲート メソッドを実装する

    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
    {
        [self.tableView reloadData];
    }
    
  4. 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;
    }
    
  5. 新しいオブジェクトを永続化するたびに、デリゲートが自動的にトリガーされ、新しいオブジェクトを含むテーブルをリロードします。

編集:

時間を節約したい場合は、新しいマスター/ディテール アプリケーションを作成します。MasterViewController には、ステップ 1 のソース コードと、ステップ 3 のスムーズなアニメーションのソース コードがあります。

于 2013-03-25T12:20:22.937 に答える