0

Core Data を使用する最新の SDK と XCode 4.5.2 で iOS アプリを開発しています。

には、と のUIViewController2 つUITableViewshopsListありproductsListます。Core Dataを使用したいのですが、使用するのはこれが初めてです。そこで、Xcode マスター ディテール テンプレートの使用を開始し、次のコードを見つけましたMasterViewController.m

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    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:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

このUIViewControllerインターフェースがある場合:

@interface FirstViewController : UIViewController<CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
{
    CLLocationManager* locationManager;
    BOOL isMenuHidden;
    BOOL isShopsListOpen;
    BOOL isProductsListOpen;
    long int selectedShopRow;
    long int selectedProductRow;
    NSIndexPath* shopCheckedIndexPath;
    NSIndexPath* productCheckedIndexPath;
}

@property (unsafe_unretained, nonatomic) IBOutlet UITableView *shopsList;
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *productsList;

[ ... ]

shopとがある場合product NSManagedObject

- (NSFetchedResultsController *)fetchedResultsControllerこれら2つを使用するためにどのように適応できUITableViewますか?

私はおそらくする必要がありNSFetchedResultsControllerますよね?

4

2 に答える 2

3

まず、アプリ デリゲートで viewController の managedObjectContext を割り当てます。

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *productsListFetchedResultsController;
@property (strong, nonatomic) NSFetchedResultsController *shopsListFetchedResultsController;

データ モデルに 2 つのエンティティを作成し、必要に応じて appdelegate に利用可能なデータを入力します。

viewWillAppear では、2 つのエンティティを 2 つの NSFetchedResultsController にフェッチし、基本的な fetchResult ブロック コードをユーティリティ パネルにドラッグして、コンテキストを self.manageObjectContext に変更し、エンティティ名を関連付け名に変更します。

于 2013-01-23T09:36:39.970 に答える
0

解決策は、 の 2 つのインスタンスを使用することですNSFetchedResultsController。私のアプリの1つに同様の設定があります。

@property取得した結果コントローラをビュー コントローラのとして定義することをお勧めします。

重要: などの FRC デリゲート コールバックでどのコントローラーが呼び出されているかを確認することを忘れないでくださいcontroller:didChangeObject:...

于 2013-01-23T12:02:24.647 に答える