简体   繁体   English

核心数据NSSet返回空对象数组

[英]Core Data NSSet returning empty array of objects

UPDATE: I had ghost entries in my database of games without players from before I implemented Players at all. 更新:在我完全没有使用Player之前,我的游戏数据库中有没有Ghost的Ghost条目。 It was returning those entries not the latest entries with the updated Model 它返回的是这些条目,而不是更新了模型的最新条目。

I am working to get a Core Data database working and having some trouble with relationships. 我正在努力使核心数据数据库正常工作,并且在关系方面遇到一些麻烦。 I'm very new to the whole thing so don't put anything past me I may no even understand the basics. 我对整个事情都很陌生,所以不要丢下任何东西,我可能甚至都不了解基本知识。 But here we go anyway. 但是无论如何,我们在这里。

This is where I create a new Game Entity. 这是我创建新游戏实体的地方。 The Players is a to-may relationship to Several Player entities that were selected and stored in an array. 播放器与选定并存储在数组中的多个播放器实体之间存在可能的关系。

    Game *newGame = [NSEntityDescription insertNewObjectForEntityForName:@"Game" inManagedObjectContext:[SDDataManager dataManager].managedObjectContext];
    [newGame setGameType:@"multipleChoice"];
    [newGame setDate:[NSDate date]];
    NSSet *playersSet = [NSSet setWithArray:players];
    [newGame setPlayers:playersSet];
    [newGame setCards:[NSKeyedArchiver archivedDataWithRootObject:selectedCards]];

    NSError *error;
    [[SDDataManager dataManager].managedObjectContext save:&error];
    NSLog(@"New Game Error: %@",[error localizedDescription]);

The problem is that when I call it out of the database like this: 问题是当我像这样从数据库中调用它时:

NSFetchRequest *requestSavedGame = [NSFetchRequest fetchRequestWithEntityName:@"Game"]; NSFetchRequest * requestSavedGame = [NSFetchRequest fetchRequestWithEntityName:@“ Game”]; [requestSavedGame setFetchLimit:1]; [requestSavedGame setFetchLimit:1];

NSError *error;
NSArray *loadedGame = [[SDDataManager dataManager].managedObjectContext executeFetchRequest:requestSavedGame error:&error];
NSLog(@"Load Game Error: %@",[error localizedDescription]);

Game *game = [loadedGame objectAtIndex:0];
NSLog(@"Players Set: %@",game.players);
NSLog(@"Players: %@",[game.players allObjects]);

The Players: is empty?? 玩家:是空的吗? It returns this exactly: 它确切地返回此:

Players Set: Relationship 'players' fault on managed object (0xd023b70) <Game: 0xd023b70> (entity: Game; id: 0xd023180 <x-coredata://E6A82377-31D2-4D11-B890-B3FDC5A03E0E/Game/p1> ; data: {
cards = <62706c69 73743030 d4010203 0405086e 6f542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572>;
currentPlayer = 0;
date = "2012-03-27 18:20:07 +0000";
gameType = multipleChoice;
players = "<relationship fault: 0xd01fd60 'players'>";
})

Players: ( )

I have no understanding why the players is a full array, then a full set when it goes in but when it comes out the [set allobjects] returns an empty array... 我不明白为什么玩家是一个完整的数组,然后是一个完整的数组,但是当它出现时,[set allobjects]返回一个空数组...

I would suggest you to use valueForKey for whatever key. 我建议您将valueForKey用于任何键。

In core data fault is not an error, it means you are trying to access from core data which does not exist. 在核心数据中,错误不是错误,这意味着您正在尝试从不存在的核心数据进行访问。 and i would say you should use setPropertiesToFetch which specifies which properties should be returned by the fetch. 我会说你应该使用setPropertiesToFetch ,它指定应该由获取返回的属性。

The message saying that it is a fault does not mean that it is empty. 消息说,这是一个错误并不意味着它是空的。 It simply means that the data has not been loaded from the database. 这仅表示尚未从数据库加载数据。 If you access it like normal, you should see your data there. 如果您像平常一样访问它,则应该在那里查看数据。 Try this and see what it says: 试试这个,看看它说什么:

NSLog(@"Players count: %i", [game.players count]);

Read about faults here: Core Data Programming Guide - Faulting and Uniquing 在此处阅读有关故障的信息: 核心数据编程指南-故障和Uniqueing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM