日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iPhone开发教程之Core Data 常见问题的总结

發(fā)布時間:2025/3/20 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iPhone开发教程之Core Data 常见问题的总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、如果想把模擬其中保存的數(shù)據(jù)在真機中顯示出來,可以在 AppDelegate 里用下面方法:

  模擬器保存完數(shù)據(jù),然后進入目錄:“用戶名/Library/Application Support/iPhone Simulator/4.2/Applications/" 目錄下,找到 p.sqlite,復制到軟件的工程目錄里。然后導入到工程里面,這樣可以用模擬器里面保存的數(shù)據(jù)了。

  - (NSManagedObjectModel *)managedObjectModel {

  if (managedObjectModel != nil) {

  return managedObjectModel;

  }

  managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];

  return managedObjectModel;

  }

  /**

  Returns the persistent store coordinator for the application.

  If the coordinator doesn't already exist, it is created and the application's store added to it.

  */

  - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

  if (persistentStoreCoordinator != nil) {

  return persistentStoreCoordinator;

  }

  NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"p.sqlite"];

  /*

  Set up the store.

  For the sake of illustration, provide a pre-populated default store.

  */

  NSFileManager *fileManager = [NSFileManager defaultManager];

  // If the expected store doesn't exist, copy the default store.

  if (![fileManager fileExistsAtPath:storePath]) {

  NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"p" ofType:@"sqlite"];

  if (defaultStorePath) {

  [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];

  }

  }

  NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

  persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

  NSError *error;

  if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {

  // Update to handle the error appropriately.

  NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

  exit(-1); // Fail

  }

  return persistentStoreCoordinator;

  }

  //Returns the path to the application's documents directory.

  - (NSString *)applicationDocumentsDirectory {

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

  return basePath;

  }

  二、常見錯誤 entityForName: could not locate an NSManagedObjectModel for entity name "Entity" 的解決方法:

  如果是如果是 AppDelegate.m 里面出現(xiàn)錯誤,需要這么寫

  FavoriteViewController *favoriteVC = [[FavoriteViewController alloc] initWithNibName:nil bundle:nil];

  favoriteVC.managedObjectContext = self.managedObjectContext;

  如果是兩個界面跳轉,主要是出現(xiàn)在 iOS 4.0 以下版本,正確寫法是:

  Test.h

  NSManagedObjectContext *managedObjectContext;

  NextTest *next;

  Test.m

  next.managedObjectContext = self.managedObjectContext;

  [self.navigationController pushViewController: next animated:YES];

  錯誤寫法:

  NextTest *next = [[NextTest alloc] init];

  next.managedObjectContext = self.managedObjectContext;

  [self.navigationController pushViewController: next animated:YES];

  [next release];

  三、條件過濾:

  選擇 cid=1 的數(shù)據(jù)

  - (NSFetchedResultsController *)fetchedResultsController {

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cid=1"];

  [fetchRequest setPredicate:predicate];

  }

  四、通用代碼,在 viewcontroller 里面會用到的。

  TestViewController.h

  @interface TestViewController : UITableViewController {

  NSFetchedResultsController *fetchedResultsController;

  NSManagedObjectContext *managedObjectContext;

  }

  @property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

  @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

  - (void)configureCell:(UITableViewCell *)cell withCategory:(Category *)category;

  @end

  TestViewController.m

  #pragma mark -

  #pragma mark fetchedResultsController

  - (NSFetchedResultsController *)fetchedResultsController {

  if (fetchedResultsController != nil) {

  return fetchedResultsController;

  }

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

  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:managedObjectContext];

  [fetchRequest setEntity:entity];

  NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

  NSArray *arraySortDescriptor = [[NSArray alloc] initWithObjects:nameDescriptor, nil];

  [fetchRequest setSortDescriptors:arraySortDescriptor];

  NSPredicate *predicate = [NSPredicate predicateWithFormat:cidString];

  [fetchRequest setPredicate:predicate];

  NSFetchedResultsController *frController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest

  managedObjectContext:managedObjectContext

  sectionNameKeyPath:@"name"

  cacheName:@"Root"];

  frController.delegate = self;

  self.fetchedResultsController = frController;

  [frController release];

  [fetchRequest release];

  [nameDescriptor release];

  return fetchedResultsController;

  }

  - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

  // The fetch controller is about to start sending change notifications, so prepare the table view for updates.

  [self.tableView beginUpdates];

  }

  - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

  UITableView *tableView = self.tableView;

  switch (type) {

  case NSFetchedResultsChangeInsert:

  [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

  break;

  case NSFetchedResultsChangeDelete:

  [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

  break;

  case NSFetchedResultsChangeMove:

  [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

  [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

  break;

  case NSFetchedResultsChangeUpdate:

  [self configureCell:[tableView cellForRowAtIndexPath:indexPath] withCategory:anObject];

  break;

  default:

  break;

  }

  }

  - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

  switch(type) {

  case NSFetchedResultsChangeInsert:

  [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

  break;

  case NSFetchedResultsChangeDelete:

  [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

  break;

  }

  }

  - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

  // The fetch controller has sent all current change notifications, so tell the table view to process all updates.

  [self.tableView endUpdates];

  }

總結

以上是生活随笔為你收集整理的iPhone开发教程之Core Data 常见问题的总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。