なぜこれが起こっているのか理解できません。コンテキストに追加できます。しかし、オブジェクトを取得すると、正しい数のオブジェクトが返されますが、オブジェクトの属性はnullです。
このコードで3つのインスタンスを追加しています:
+(BOOL)addStoreWithID:(NSNumber *)ID Latitude:(NSNumber *)latitude Longitude:(NSNumber *)longitude Name:(NSString *)name {
Stores *store = (Stores *)[NSEntityDescription
insertNewObjectForEntityForName:@"Stores"
inManagedObjectContext:[[SharedResources instance] managedObjectContext]];
store.ID = ID;
store.Latitude = latitude;
store.Longitude = longitude;
store.Name = name;
NSError *error;
if(![[[SharedResources instance] managedObjectContext] save:&error])
{
//Handle the error
return NO;
}
return YES;
}結果が表示されます:2010-03-07 19:19:37.060 GamePouch_iPhone [11337:207]ストア名はStarbucks(gdb)続行2010-03-07 19:19:37.933 GamePouch_iPhone [11337:207]ストア名はDunkinドーナツ(gdb)続行2010-03-07 19:19:38.717 GamePouch_iPhone [11337:207]ストア名はKrispyKremeです
このコードは3回アクセスされ、どの属性もnilではないことを確認しました。
次に、それを取得しようとすると、次のコードを使用します。
+(NSMutableArray *)fetchAllObjects {
NSFetchRequest *request;
request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Stores" inManagedObjectContext:[[SharedResources instance] managedObjectContext]];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error;
NSMutableArray *array = [[[SharedResources instance] managedObjectContext] executeFetchRequest:request error:&error];
[request release];
[sortDescriptor release];
[sortDescriptors release];
for(int i=0;i<3;i++)
{
Stores *tempStore = (Stores *)[array objectAtIndex:i];
NSLog(@"store name is %@",[tempStore Name]);
}
return array;
}
結果が表示されます:2010-03-07 19:21:00.504 GamePouch_iPhone [11337:207]ストア名は(null)(gdb)続行2010-03-07 19:21:01.541 GamePouch_iPhone [11337:207]ストア名は(null)(gdb)続行2010-03-07 19:21:02.503 GamePouch_iPhone [11337:207]ストア名は(null)
読んでくれてありがとう。どんな助けでも大歓迎です。
ありがとうBakhtiyaruddin