2016-02-29 44 views
0

您好我正在尝试学习firebase,现在Parse将被关闭,我在将用户添加到我的数据库时遇到了一些麻烦。FireBase - 将用户添加到用户列表

按照网站上的iOS的教程,我第一次听的使用用户列表:

- (void)setUpRootReference { 
self.rootReference = [[Firebase alloc] initWithUrl:@"firebaseurl"]; 
// Attach a block to read the data at our posts reference 
[self.rootReference observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { 
    NSLog(@"%@", snapshot.value); 
    if (snapshot.value != [NSNull null]) { 
     userDic = [snapshot.value objectForKey:@"users"]; 
     NSLog(@"user dictionary: %@", userDic); 
    } 
} withCancelBlock:^(NSError *error) { 
    NSLog(@"%@", error.description); 
}]; 
}  

然后在用户已在使用Instagram的成功地登录我想给新用户添加到现有的用户列表。但是现在它只是被重写,并且我总是留下一个用户。

这里是用户后,我完成代码注册:

//create a path on firebase for users 
     Firebase *usersRef = [self.rootReference childByAppendingPath: @"users"]; 

     [userDic setValue:newUserDic forKey:newUser.instagramHandle]; 
     [usersRef setValue:userDic]; 

有谁知道我能一定的价值,追加到用户与擦除,并创建一个新的每一次的现有的字典?

感谢

UPDATE:

我想出如何使它发挥作用。每次你创建一个新用户,你想使一个新的参考用户

解决方案:

//create a path on firebase for users 
     Firebase *allUsersRef = [self.rootReference childByAppendingPath: @"users"]; 
     Firebase *newUserRef = [allUsersRef childByAppendingPath:newUser.instagramHandle]; 
     [newUserRef setValue:newUserDic]; 

现在会有一个新的用户词典添加关键是用户的Instagram处理

+0

有关firebase的Idk,但要修改现有字典,您必须使用“NSMutableDictionary”而不是“NSDictionary”。 – NSNoob

+0

这包括在[存储用户数据的文档](https://www.firebase.com/docs/ios/guide/user-auth.html#section-storing)中,它确实使用了'childByAppendingPath'。整个iOS指南非常值得一读。 –

+0

另请参阅以下与昨天密切相关的问题:http://stackoverflow.com/questions/35678559/new-user-vs-old-firebase-facebook-authentication –

回答

0
NSDictionary *nickname = @{ 
    @"nickname": @"Igor Kuznetsov", 
}; 
[userRef updateChildValues: nickname]; 

有一种叫做updateChildValues的方法。尝试这样的事情。 [userRef updateChildValues:nickname]; 我上面的例子会改变用户的昵称,在你的情况下,你可以改变你有兴趣的属性的昵称。