我可能會在使用單詞singleton的標題中使用我的術語不正確。 我現在在尋找一種很好的技術。我有一個名爲user的實體,用於存儲用戶登錄的數據,例如用於發出服務器請求的會話密鑰。我只想讓這些實體中的一個存在。有這樣的標準技術嗎?CoreData中的單身實體
我有什麼到目前爲止是這樣的
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"UserEntity" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
// Deal with error...
}
if ([array count]==0) {
//first run of app
}else if([array count]==1)
{
// id like the code to enter here after every app run except for the first one
}else
{
//dont want this to happen
}
這是一個非常好的解決方案。我之前已經使用過這個宏,完全忘了它。 – dubbeat 2011-05-13 10:31:43
在Matt Gallager的文章中似乎沒有任何特定於Core Data的內容。 – ThomasW 2016-03-18 08:37:02
一致認爲,這個答案與Core Data無關,如果您是否可以使用約束來確保數據存儲中只存在一個特定實體的實例,它應該真正解決該問題。 – 2017-11-09 01:33:57