2009-08-20 187 views
0

我想開發一個測試核心數據應用程序,所以我可以在我的主應用程序中使用它。但即時通訊有問題獲取存儲的數據。核心數據NSFetchRequest問題

我有一個實體「用戶」有兩個屬性:名稱(字符串)&電子郵件(字符串) 我可以成功地填充我的數據存儲文件... 這裏是使用下面的代碼IM。 任何幫助將不勝感激。

NSManagedObjectContext *managedObjectContext; 
NSError *error; 

NSString *basePath = @"/Users/Jamie/Desktop"; 
NSURL *storePath = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent:@"datastore.xml"]]; 

NSLog(@"Data Store path: %@", storePath); 

NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]]; 
if(![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:storePath options:nil error:&error]) { 
    NSLog(@"Unable to load Core Data Store"); 
    NSLog(@"ERROR: %@", error); 
    return 1; 
} else { 
    NSLog(@"Loaded Core Data Store"); 
} 

managedObjectContext = [[NSManagedObjectContext alloc] init]; 
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]; 

// Add data to store. 
/* 
NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:managedObjectContext]; 
[mo setValue:@"jamie" forKey:@"name"]; 
[mo setValue:@"[email protected]" forKey:@"email"]; 
if([managedObjectContext save:&error]) { 
    NSLog(@"Saved data to store"); 
} else { 
    NSLog(@"Unable to save data to store"); 
    NSLog(@"ERROR: %@", error); 
} 
*/ 

// Fetch all the data - not worrying about using a predicate. 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 

NSArray *results = [managedObjectContext executeFetchRequest:request error:&error]; 

回答

1

固定 - 如果拆分出來的零件到單獨的方法,它的所有作品:)