【发布时间】:2016-09-03 00:57:11
【问题描述】:
我正在尝试使用 Firebase 在 IOS 中建立一个聊天室。当我在 firebase 控制台的模拟器中进行测试时,一切正常,但在 IOS 应用程序中无法正常工作。
我使用的结构是这样的
root/members/room-id/auth.uid/userdata
root/messages/room_id/messages/message
我在 IOS 中创建了一个新房间
- (void)initRoom
{
NSMutableDictionary *mdata = [[NSMutableDictionary alloc] init];
FIRUser *user = [FIRAuth auth].currentUser;
mdata[@"uid"] = user.uid;
// get the key for the new room
roomKey = [[_rootRef child:@"messages"] childByAutoId].key;
if (roomKey != nil) {
// add auth.uid to member/roomkey
NSString *memberID = [[[_rootRef child:@"members"] child: roomKey ] child: user.uid ].key;
[[[[_rootRef child:@"members"] child: roomKey ] child: memberID ] setValue:mdata];
[self addMemberToRoom];
}
}
并为该房间添加其他成员(目前使用硬编码的 uid):
-(void) addMemberToRoom{
NSString *memberID = [[[_rootRef child:@"members"] child: roomKey ] child: @"QLfRoGpoCjWpzira7fljBj8g3EJ3"].key;
NSMutableDictionary *mdata = [[NSMutableDictionary alloc] init];
mdata[@"uid"] = @"QLfRoGpoCjWpzira7fljBj8g3EJ3";
if (memberID !=nil) {
[[[[_rootRef child:@"members"] child: roomKey ] child: memberID ] setValue:mdata];
}
// start listener
[self observeMessages];
}
然后像这样启动监听器:
-(void)observeMessages
{
_msgHandle = [[[_rootRef child:@"messages"] child: roomKey] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
NSDictionary<NSString *, NSString *> *message = snapshot.value;
NSString *name = message[@"senderId"];
NSString *text = message[@"message"];
[self addMessagewithId:name andText:text];
// animates the receiving of a new message on the view
[self finishReceivingMessage];
}];
}
当我设置这样的规则时,将消息添加到数据库并在应用程序中查看它,这一切都很好:
{
"rules": {
"members": {
".read": "auth !== null",
".write": "auth !== null",
},
"messages": {
".write":"auth !== null",
".read": "auth !== null",
"$room_id": {
}
}
}
}
现在,由于我不希望每个人都能够阅读我设置规则的消息:
{
"rules": {
"members": {
".read": "auth !== null",
".write": "auth !== null",
},
"messages": {
".write": "auth !== null",
"$room_id": {
".read": "root.child('members/'+$room_id+'/'+auth.uid).exists()",
}
}
}
}
当我这样做时,IOS 应用程序中的侦听器将不会收到任何呼叫。在 firebase 控制台中使用模拟器检查从应用程序创建的 room_id 和用户,读取标记为 OK。
我的假设是,侦听器在创建新成员之前初始化,但即使延迟初始化也无济于事。
你能帮我指出正确的方向吗?非常感谢!
【问题讨论】: