简体   繁体   English

Realm写入中的行为不一致

[英]Inconsistent behaviour in Realm write

I am having trouble with a write operation in Realm. 我在Realm中进行写操作时遇到麻烦。 All other read/writes are working perfectly as expected apart from this one and I am stumped as to what may be causing this issue. 除此以外,所有其他读/写均按预期工作,并且我可能会导致此问题的原因很令人困惑。

I have a game object in realm and it has an array which holds score type of objects. 我在领域中有一个游戏对象,它具有一个数组,其中包含对象的得分类型。 When the user joins a game, the API responds with an array of placeholder score objects. 当用户加入游戏时,API会使用一系列占位符得分对象进行响应。 I am trying to add these scores to the game object. 我正在尝试将这些分数添加到游戏对象中。 Based on different flows the game object may or may not have yet been written to Realm . 基于不同的流程,游戏对象可能已经写入或可能尚未写入Realm I am using the following code to add scores to the game object. 我正在使用以下代码向游戏对象添加分数。 Game and Scores here are subclasses of RLMObject GameScoresRLMObject子类

[[RLMRealm defaultRealm] transactionWithBlock:^{
    RLMRealm *realm = [RLMRealm defaultRealm];
    //NSArray *scores contains all score objects to be added
    Game game = [Game createOrUpdateInRealm:realm withValue:aGame];

    for (Score *aScore in scores) {
        Score *rlmScore = [Score createOrUpdateInRealm:realm withValue:aScore];
        [game.scores addObject:rlmScore];
    }

    //Pass game to next ViewController for display
}];

The problem is that by the end of this transaction block the game.scores array has 1 score object even though the loop has run more than once since the scores array has more than 1 object. 问题在于,在该事务处理块结束时,即使game.scores数组具有1个得分对象,即使循环已经运行了不止一次,因为scores数组具有多个对象。 Even weirded is the fact that this problem does not always happen. 甚至很奇怪的是,这个问题并不总是会发生。 No other operation is going on in another thread. 没有其他操作在另一个线程中进行。 The UI is blocked and waiting for the API response at this point and only continues after this write transaction. 此时,UI被阻止并等待API响应,并且仅在此写事务之后才继续。

Any help would be appreciated 任何帮助,将不胜感激

[[RLMRealm defaultRealm] transactionWithBlock:^{
    RLMRealm *realm = [RLMRealm defaultRealm];
    //NSArray *scores contains all score objects to be added
    Game game = [Game createOrUpdateInRealm:realm withValue:aGame];

    NSMutableArray *rlmScores = [NSMutableArray array];
    for (Score *aScore in scores) {
        Score *rlmScore = [Score createOrUpdateInRealm:realm withValue:aScore];
        [rlmScores addObject:rlmScore];
    }

    [game.scores addObjects:rlmScores];

    //Pass game to next ViewController for display
}];

Adding the Realm score objects to an array and then adding the array to game.scores RLMArray worked. 将Realm得分对象添加到数组,然后将该数组添加到game.scores RLMArray。 I have no idea why this worked and adding the objects one by one in a loop won't work. 我不知道为什么这行得通,并且在循环中一个接一个地添加对象是行不通的。 I am posting the solution that worked for me if anyone else runs into a similar problem. 如果有人遇到类似问题,我将发布对我有用的解决方案。

Please feel free to provide a better solution or an explanation for the issue. 请随时为该问题提供更好的解决方案或解释。

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

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