NSMutableDictionary在实例化和初始化后仍然是零

时间:2015-08-28 10:13:36

标签: objective-c nsmutabledictionary

以下是每次收到发言者信息时调用的方法。

- (void)handleGroupingFromReceivedSpeakerInfo:(SpeakerInfo *)speakerInfo
{
    GroupInfo *groupInfo = [_IP_GroupInfo_Map objectForKey:speakerInfo.mGroupIP];

    if(groupInfo == nil)
    {
        groupInfo = [[GroupInfo alloc] init];
        groupInfo.mIP = speakerInfo.mGroupIP;
        groupInfo.isTemp = YES;

        NSLog(@"HandleGrouping: New temp group %@", groupInfo.mIP);
    }

    if(groupInfo.isTemp)
    {
        if(![groupInfo.mSpeakerIDList containsObject:[@(speakerInfo.mID) stringValue]])
        {
            [groupInfo.mSpeakerIDList addObject:[@(speakerInfo.mID) stringValue]];

            NSLog(@"HandleGrouping: added new member with id %@ temp group with ip %@", [@(speakerInfo.mID) stringValue], groupInfo.mIP);

            [_IP_GroupInfo_Map setObject:groupInfo forKey:groupInfo.mIP];
        }
    }
}

现在我将解释一下代码的作用。

  • 每次收到发言人信息时,都会获得使用密钥 speakerInfo.mGroupIP

  • 获得的存储GroupInfo
  • 如果 groupInfo nil ,则简单实例化一个新的,然后将扬声器信息的ID添加到 groupInfo

  • 如果不是,请继续将speakerInfo添加到现有的GroupInfo

好的,问题就在这里,请看一下日志(请不要介意其他日志消息,只有以&#34开头的消息; HandleGrouping: &#34):

Discovered speaker with id 6494094734139439904
2015-08-28 17:41:53.756 soulbeats[13558:3307] Added speakerID: 6494094734139439904 to AllSpeakersIDList
2015-08-28 17:41:53.758 soulbeats[13558:1803] Discovered speaker with id 72243140485806144
2015-08-28 17:41:53.763 soulbeats[13558:3307] Old speaker info is null
2015-08-28 17:41:53.765 soulbeats[13558:1803] Added speakerID: 72243140485806144 to AllSpeakersIDList
2015-08-28 17:41:53.770 soulbeats[13558:3307] HandleGrouping: New temp group 239.11.11.12
2015-08-28 17:41:53.772 soulbeats[13558:1803] Old speaker info is null
2015-08-28 17:41:53.777 soulbeats[13558:3307] HandleGrouping: added new member with id 6494094734139439904 temp group with ip 239.11.11.12
2015-08-28 17:41:53.779 soulbeats[13558:1803] HandleGrouping: New temp group 239.11.11.12
2015-08-28 17:41:53.785 soulbeats[13558:3307] Discovered speaker with id 7782687177520836128
2015-08-28 17:41:53.788 soulbeats[13558:1803] HandleGrouping: added new member with id 72243140485806144 temp group with ip 239.11.11.12
2015-08-28 17:41:53.794 soulbeats[13558:3307] Added speakerID: 7782687177520836128 to AllSpeakersIDList
2015-08-28 17:41:53.802 soulbeats[13558:3307] Old speaker info is null
2015-08-28 17:41:53.807 soulbeats[13558:3307] HandleGrouping: added new member with id 7782687177520836128 temp group with ip 239.11.11.12
2015-08-28 17:41:55.711 soulbeats[13558:60b] SendBrowseCmd: browse again!!!
2015-08-28 17:41:55.719 soulbeats[13558:60b] SendBrowseCmd: refresh!!!
2015-08-28 17:41:55.721 soulbeats[13558:60b] Refresh: found one temp group with ip 239.11.11.12 with speakerIDListCount 2

从上面可以看出, groupInfo 已实例化 TWICE 。您可以看到日志消息" New temp group 239.11.11.12"出现 TWICE

2015-08-28 17:41:53.770 soulbeats[13558:3307] HandleGrouping: New temp group 239.11.11.12
2015-08-28 17:41:53.772 soulbeats[13558:1803] Old speaker info is null
2015-08-28 17:41:53.777 soulbeats[13558:3307] HandleGrouping: added new member with id 6494094734139439904 temp group with ip 239.11.11.12
2015-08-28 17:41:53.779 soulbeats[13558:1803] HandleGrouping: New temp group 239.11.11.12

我不明白为什么会这样。我使用相同的密钥获取groupInfo对象,这是收到的speakerInfo的IP, 239.11.11.12 。所以我期待在第一次调用这个方法时,groupInfo应该是nil。但之后,因为我已经在其中添加了一个speakerInfo.mID并再次将其设置为 _IP_GroupInfo_Map 字典,所以groupInfo在下次调用此方法时不应该为nil。

其他信息:

我用这种方式声明NSMutableDictionary:

@property (nonatomic, strong) NSMutableDictionary *IP_GroupInfo_Map;

我该怎么做才能解决这个问题?请帮忙。非常感谢。

---------更新:添加了NSLog(@"%@",self)--------------

2015-08-28 18:41:28.497 soulbeats[13652:5d03] Discovered speaker with id 7782687177520836128
2015-08-28 18:41:28.505 soulbeats[13652:1803] <MultiroomPlay: 0x1760ba90>
2015-08-28 18:41:28.507 soulbeats[13652:3903] <MultiroomPlay: 0x1760ba90>
2015-08-28 18:41:28.508 soulbeats[13652:5d03] Added speakerID: 7782687177520836128 to AllSpeakersIDList
2015-08-28 18:41:28.516 soulbeats[13652:1803] HandleGrouping: New temp group 239.11.11.12
2015-08-28 18:41:28.518 soulbeats[13652:3903] HandleGrouping: New temp group 239.11.11.12
2015-08-28 18:41:28.519 soulbeats[13652:5d03] Old speaker info is null
2015-08-28 18:41:28.527 soulbeats[13652:1803] HandleGrouping: added new member with id 6494094734139439904 temp group with ip 239.11.11.12
2015-08-28 18:41:28.530 soulbeats[13652:3903] HandleGrouping: added new member with id 72243140485806144 temp group with ip 239.11.11.12
2015-08-28 18:41:28.531 soulbeats[13652:5d03] <MultiroomPlay: 0x1760ba90>
2015-08-28 18:41:28.543 soulbeats[13652:5d03] HandleGrouping: added new member with id 7782687177520836128 temp group with ip 239.11.11.12

可以看出,id正好是 SAME

-------另一个更新:添加了关于如何初始化类的代码-------

+ (MultiroomPlay *)getMultiroomPlay
{
    static MultiroomPlay *sharedMultiroomPlay;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken,
    ^{
        sharedMultiroomPlay = [[MultiroomPlay alloc] init];
    });

    return sharedMultiroomPlay;
}

- (id)init
{
    self = [super init];

    if (self)
    {
        [self openSocket];

        _allSpeakersIDList = [[NSMutableArray alloc] init];
        _allPermGroupsIDList = [[NSMutableArray alloc] init];
        _allGroupsIPList = [[NSMutableArray alloc] init];
        _allUngroupedSpeakersList = [[NSMutableArray alloc] init];
        _allUngroupedPermGroupsList = [[NSMutableArray alloc] init];
        _allTempGroupsList = [[NSMutableArray alloc] init];
        _tempGroupMemberRemoveSeqList = [[NSMutableArray alloc] init];

        _ID_TCPConnection_Map = [[NSMutableDictionary alloc] init];
        _ID_SpeakerInfo_Map = [[NSMutableDictionary alloc] init];
        _ID_PermGroupInfo_Map = [[NSMutableDictionary alloc] init];
        _IP_GroupInfo_Map = [NSMutableDictionary dictionary]; // <--Look here
        _IP_PlayerInfo_Map = [[NSMutableDictionary alloc] init];
        _IP_PlayBackTimer_Map = [[NSMutableDictionary alloc] init];
        _IP_FinishSeqNum_Map = [[NSMutableDictionary alloc] init];
    }

    return self;
}

---------为GroupInfo的值添加了日志------------

2015-08-28 19:07:29.043 soulbeats[13709:3307] Discovered speaker with id 6494094734139439904
2015-08-28 19:07:29.046 soulbeats[13709:1803] Discovered speaker with id 72243140485806144
2015-08-28 19:07:29.050 soulbeats[13709:3307] Added speakerID: 6494094734139439904 to AllSpeakersIDList
2015-08-28 19:07:29.052 soulbeats[13709:1803] Added speakerID: 72243140485806144 to AllSpeakersIDList
2015-08-28 19:07:29.058 soulbeats[13709:3307] Old speaker info is null
2015-08-28 19:07:29.060 soulbeats[13709:1803] Old speaker info is null
2015-08-28 19:07:29.066 soulbeats[13709:3307] <MultiroomPlay: 0x1460e920>
2015-08-28 19:07:29.069 soulbeats[13709:1803] <MultiroomPlay: 0x1460e920>
2015-08-28 19:07:29.074 soulbeats[13709:3307] Value of Groupinfo: (null)
2015-08-28 19:07:29.076 soulbeats[13709:1803] Value of Groupinfo: (null)
2015-08-28 19:07:29.081 soulbeats[13709:3307] HandleGrouping: New temp group 239.11.11.12
2015-08-28 19:07:29.083 soulbeats[13709:1803] HandleGrouping: New temp group 239.11.11.12
2015-08-28 19:07:29.089 soulbeats[13709:3307] HandleGrouping: added new member with id 6494094734139439904 temp group with ip 239.11.11.12
2015-08-28 19:07:29.091 soulbeats[13709:1803] HandleGrouping: added new member with id 72243140485806144 temp group with ip 239.11.11.12
2015-08-28 19:07:29.098 soulbeats[13709:3307] Discovered speaker with id 7782687177520836128
2015-08-28 19:07:29.250 soulbeats[13709:3307] Added speakerID: 7782687177520836128 to AllSpeakersIDList
2015-08-28 19:07:29.332 soulbeats[13709:3307] Old speaker info is null
2015-08-28 19:07:29.380 soulbeats[13709:3307] <MultiroomPlay: 0x1460e920>
2015-08-28 19:07:29.394 soulbeats[13709:3307] Value of Groupinfo: <GroupInfo: 0x1589aad0>
2015-08-28 19:07:29.400 soulbeats[13709:3307] HandleGrouping: added new member with id 7782687177520836128 temp group with ip 239.11.11.12

2 个答案:

答案 0 :(得分:1)

事实证明你的程序多线程的。用@synchronized围绕代码并获得预期的输出显示了这一点。

@synchronized导致块以原子方式执行。在两个线程可以输入你的代码之前,两者都发现你的字典是nil并且都实例化它,现在两个(或更多)线程每个都在synchronized块的顶部等待,直到其他一个(s)退出。

然而,不是仅仅接受&#34;它现在可以正常工作&#34;,你应该尝试找出当你没想到它时多线程的原因!

(本期的部分内容已在chat中讨论过。)

答案 1 :(得分:0)

在适当的初始化方法中添加以下行

_IP_GroupInfo_Map = [NSMutableDictionary dictionary];