3

詳細はコメント欄にあります。

次のコード:

// Perform the fetch...
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

// Confirm that objects were fetched by counting them...
NSLog(@"Number of Objects = %i",
      [[fetchedResultsController fetchedObjects] count]);

// Confirm that sections exist by counting them...
NSLog(@"Numbers of Sections = %i",
      [[fetchedResultsController sections] count]); 

for (id section in [fetchedResultsController sections]) {
    // Count number of objects in each section
    // _The fact that this outputs 0 is the first sign of trouble_
    NSLog(@"Number of Objects in Section = %i", [section numberOfObjects]);
}

for (Reminder *reminder in [fetchedResultsController fetchedObjects]) {
    // Confirm that the objects fetched are in fact real objects
    // by accessing their "textContent" property...
    NSLog(@"textContent=%@", reminder.textContent);

    // Show that the fetched objects are being returned 
    // with a (null) indexPath...
    // _The second sign of trouble..._
    NSLog(@"IndexPath=%@",
          [fetchedResultsController indexPathForObject:reminder]);
}

NSUInteger indexArr[] = {0,0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr 
                                                    length:2];

// _Application crashes on this line because the fetched 
// objects do not have indexPaths_
Reminder *testReminder = (Reminder *)[fetchedResultsController 
                                      objectAtIndexPath:indexPath]; 
NSLog(@"textContent = %@", testReminder.textContent);

結果は次のようになります。

2010-07-17 00:48:41.865 Reminders[27335:207] Number of Objects = 3
2010-07-17 00:48:41.867 Reminders[27335:207] Numbers of Sections = 1
2010-07-17 00:48:41.868 Reminders[27335:207] Number of Objects in Section = 0
2010-07-17 00:48:41.870 Reminders[27335:207] textContent=Imported Object 3
2010-07-17 00:48:41.871 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.873 Reminders[27335:207] textContent=Imported Object 2
2010-07-17 00:48:41.873 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.874 Reminders[27335:207] textContent=Imported Object 1
2010-07-17 00:48:41.875 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.887 Reminders[27335:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

任意のアイデアをいただければ幸いです。参考までに、出発点として別のテンプレートを使用すると、上記のコードは別のアプリケーションで完全に機能します。つまり、「ウィンドウベースのアプリケーション」テンプレートを使用すると、コードが失敗します。「ナビゲーションベースのアプリケーション」を使用すると、コードは期待どおりに機能します。

更新: TechZenは、問題の原因が私のリマインダーエンティティであるかどうか疑問に思っていました。これを調べるのは良い考えだと思ったので、次のことを行いました。

  1. 「ウィンドウベースのアプリケーション」と「ナビゲーションベースのアプリケーション」の2つのデフォルトテンプレートアプリケーションを作成します(両方ともCore Dataが有効になっています)。

  2. 上記のテストを実行するために必要な最小限のコードをNavベースからWindowベースにコピーしました(ほとんどの場合、「xcdatamodel」ファイル、fetchedresultscontroller、およびテストオブジェクトを追加する方法)。

上記のコードは、新しい「リマインダーエンティティフリー」ウィンドウベースのアプリケーションでも失敗します。(この新しいテストアプリケーションには、実際には(テストコードの外に)自分で作成したコードはありません。すべて、テンプレートコードをカットアンドペーストしただけです。)

だから今、私は「ウィンドウベースのアプリケーション」を作成した後に上記のコードを実行する方法を探しています。誰かが試してみたい場合に備えて、navベースのデフォルトエンティティを使用してテストを実行するコードを次に示します。

更新TechZenが以下に示すように、空のデータベースで実行した場合でもこのコードはクラッシュするため、ウィンドウベースのアプリケーションから開始する場合は、最初にデータベースにいくつかのオブジェクトを追加してから、テストコードを追加してください。

// Confirm that objects were fetched
NSLog(@"Number of Objects = %i",
      [[fetchedResultsController fetchedObjects] count]);

// Confirm that sections exist
NSLog(@"Numbers of Sections = %i",
      [[fetchedResultsController sections] count]); 

for (id section in [fetchedResultsController sections]) {

    // Count number of objects in sections
    // _The fact that this outputs 0 is the first sign of trouble_
    NSLog(@"Number of Objects in Section = %i", [section numberOfObjects]);
}

for (NSManagedObject *managedObject in [fetchedResultsController fetchedObjects]) {

    // Confirm that the objects fetched are in fact real objects, 
    // by accessing their "timeStamp" property
    NSLog(@"TimeStamp=%@", [[managedObject valueForKey:@"timeStamp"] description]);

    // Show that the fetched objects are being returned 
    // with a (null) indexPath
    // _The second sign of trouble..._
    NSLog(@"IndexPath=%@",
          [fetchedResultsController indexPathForObject:managedObject]);
}

NSUInteger indexArr[] = {0,0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr 
                                                    length:2];

// _Application crashes on this line, because the fetched 
// objects do not have indexPaths_
NSManagedObject *managedObject = [fetchedResultsController 
                                  objectAtIndexPath:indexPath];
NSLog(@"textContent = %@", [[managedObject valueForKey:@"timeStamp"] description]);

ここでのUPDATEは、新しいカットアンドペーストされたコードを使用した場合の出力です。

2010-07-18 15:33:41.264 Reminders[30898:207] Number of Objects = 3
2010-07-18 15:33:41.266 Reminders[30898:207] Numbers of Sections = 1
2010-07-18 15:33:41.267 Reminders[30898:207] Number of Objects in Section = 0
2010-07-18 15:33:41.270 Reminders[30898:207] TimeStamp=2010-07-18 13:59:00 -0400
2010-07-18 15:33:41.271 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.272 Reminders[30898:207] TimeStamp=2010-07-18 13:59:00 -0400
2010-07-18 15:33:41.273 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.274 Reminders[30898:207] TimeStamp=2010-07-18 13:58:59 -0400
2010-07-18 15:33:41.275 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.276 Reminders[30898:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

更新この問題をSDKバージョン関連の問題に絞り込んだので、Simulator 3.2でビルドするとクラッシュし、Simulator3.1.3でビルドすると正常に動作するプロジェクトができました。しかし、UITableViewControllerを追加し、Simulator 3.2でビルドすると、再び正常に動作します。そこで、質問をするために新しいstackoverflow投稿を作成しました。UITableViewControllerなしでNSFe​​tchedResultsControllerを使用している場合、オブジェクトをどのように操作しますか?(IndexPathsは信頼できないため)。

更新[NSFetchedResultsController fetchedObjects] objectAtIndex:]この問題は、オブジェクトにアクセスするために-を使用することで(暫定的に)解決されます。

4

6 に答える 6

3

これは、-[NSFetchedResultsController fetchedObjects] objectAtIndex:]を使用してバイパスできます。

于 2010-07-22T23:13:41.907 に答える
3

NSFetchedResultsControllerメモリのみの追跡モードで使用しようとしましたか?(メモリのみの追跡:デリゲートはnil以外であり、ファイルキャッシュ名はnilに設定されています)

于 2012-03-01T17:43:48.727 に答える
2

NSLog(@ "%@"、indexPath)が常にnullを返すことを指摘するのはばかげていると思いますか?

あなたがする必要があります

NSLog(@"section %i",(int)indexPath.section);
NSLog(@"row %i",(int)indexPath.row);

そうでしょう?

于 2010-11-25T13:25:44.723 に答える
1

fetchedResultsControllerのsortDescriptorsの順序を見てください。セクションキーフィールドは、最初に並べ替える必要があります。

fetchedResultsControllerに複数のsortDescriptorがあり、最初の記述子がindexPathForObjectよりもセクションキーフィールドではない場合:メソッドはindexPathを解決できませんでした。

于 2013-03-24T15:23:20.230 に答える
0

外観の作成はNSFetchedResultsControllerどのように見えますか?

アップデート

参考までに、PasteBinを使用する代わりに、コードで質問を更新できます。

このコードのどこかで複数のスレッドを使用してアクセスしていNSFetchedResultsControllerますか?

于 2010-07-17T16:53:07.740 に答える
0

コードをコピーしてデフォルトのCoreDataナビゲーションテンプレートに貼り付け、エンティティを文字列属性がのリマインダーに変更したところ、textContent正常に実行されました。このコードまたはフェッチされた結果コントローラーのセットアップに問題はありません。

Reminder問題は、実際にはエンティティ、Reminderクラス、またはtextContent属性またはオブジェクトのいずれかにあると思います。これらのエラーは、リマインダーオブジェクトを適切に処理できないことが原因である可能性があります。


編集:

一部のオブジェクトがコンテキストに追加された後でのみ、このテストを実行するようにしてください。オブジェクトがない場合はクラッシュします。ウィンドウベースのテンプレートでテストします。

于 2010-07-18T15:28:08.737 に答える