0

私は基本的に、毎回データをダウンロードするのではなく、sqlite データベースを導入することで、アプリをより効率的にしようとしています。事前に入力された情報を含むデフォルトの sqlite ファイルを含めていません。ユーザーがいつ更新するかを選択できるという考えで、アプリに一度ダウンロードさせたいと思います。

現在、sqlite データベースが配置されていると思います。シミュレーターでチェックしたところ、データがダウンロードされ、正しくマップされました。私の見解では、getObjectsAtPath を実行しています。私がやりたいことは、同じデータを再ダウンロードするのではなく、sqlite データベースに既にあるものを使用するようにアプリに指示することです。

私の AppDelegate.m で私はやった

NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Blogs.sqlite"];
// I added the following line in an attempt to get the app to use the sqlite file already created
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"Blogs" ofType:@"sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];

[managedObjectStore createManagedObjectContexts];

// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:managedObjectStore];

// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

私のViewControllerには

[[RKObjectManager sharedManager] getObjectsAtPath:@"/post" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    // Do stuff with fetchedResultsController
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    // Show some errors
}];

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Post"     inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];


[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"post_title" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];    
[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Location"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

return _fetchedResultsController;

}

投稿する必要がある関連コードや、質問に追加できる詳細がある場合は、質問してください。読んでくれてありがとう。

4

1 に答える 1