ios - 为什么 FireBase 查询 block 中的 NSString 为 null?

标签 ios objective-c firebase objective-c-blocks nsmutabledictionary

我不明白为什么这个字符串在 FQuery block 内为空。当我在用户 key 上构建 dailyLog MutableDictionary 时,我的应用程序不断崩溃;

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM dd, YYYY"];

NSString *userID = [[self.userProfile objectForKey:@"userID"] copy];
self.logFirebase = [[Firebase alloc] initWithUrl:@"https://urlName.firebaseio.com/DailyLog"];
[[[self.logFirebase queryOrderedByPriority] queryEqualToValue:userID childKey:@"userID"] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

    if (![snapshot.value isEqual:[NSNull null]]) {
        NSMutableArray *data = [CGCFirebaseDataExtractor extractKeysAndObjects:snapshot.value];

        self.dailyLog = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"Name CONTAINS[cd] %@",[df stringFromDate:[NSDate date]]]].lastObject;
    }else{
        self.dailyLog = [[NSMutableDictionary alloc] init];
        NSLog(@"users Profile:%@",self.userProfile);
        NSLog(@"users ID :%@",[self.userProfile objectForKey:@"userID"]);

        [self.dailyLog setObject:[df stringFromDate:[NSDate date]] forKey:@"date"];
        [self.dailyLog setObject:[self.userProfile objectForKey:@"userID"] forKey:@"user"];
        [self.dailyLog setObject:[NSNumber numberWithInt:450] forKey:@"calories"];


        [[self.logFirebase childByAutoId] setValue:self.userProfile];

    }

}];

编辑: 当我记录 self.userProfile 时,我得到了我想要的正确信息。但是当我从 FQuery block 中记录 userID 本身时,它是 null

这是我更新的崩溃数据:

2015-05-08 13:21:22.709 <appname>[4160:1321097] users Profile:{
"-JoirTqCXFFDY1psLTNn" =     {
    dateRegistered = "1431010405.81792";
    userID = "304D92EF-CE77-4A10-A55F-9847153699F7";
    userName = "User's Name";
};
 }
 2015-05-08 13:21:22.709 <appname>[4160:1321097] users ID :(null)
 2015-05-08 13:21:22.714 <appname>[4160:1321097] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: user)'
  *** First throw call stack:
  (0x1852082d8 0x196a340e4 0x1850f1428 0x10007de00 0x1001454d0 0x100390fd4 0x100390f94 0x100395c28 0x1851bf7f8 0x1851bd8a0 0x1850e92d4 0x18e8ff6fc 0x189caefac 0x10007f14c 0x1970b2a08)
   libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

对不起,我是个白痴,没有仔细看你的日志。

2015-05-08 13:21:22.709 <appname>[4160:1321097] users Profile:{
"-JoirTqCXFFDY1psLTNn" =     {
    dateRegistered = "1431010405.81792";
    userID = "304D92EF-CE77-4A10-A55F-9847153699F7";
    userName = "User's Name";
};

这个对象显然是字典中的字典。第一个(外部)字典有键 "-JoirTqCXFFDY1psLTNn" 这是它唯一的键。这就是为什么尝试获取 key userID 会失败;该键是第二个(内部)字典的一部分。

关于ios - 为什么 FireBase 查询 block 中的 NSString 为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129116/

相关文章:

objective-c - iOS:替换数组数组中的对象

android - 为什么在Android Studio 3.3中登录Google时找不到default_web_client_id

javascript - Firebase 限制在没有用户登录的情况下访问其他页面

ios - 从 url 下载 PDF 文件并重复使用下载的文件

ios - 如何向字符串添加属性以返回两个值,字符串本身和另一个字符串(设置和获取)

objective-c - 将 AFNetworking POST constructingBodyWithBlock 从 Objective C 翻译成 Swift

android - 使用 datasnapshot 从 map 标记获取数据

mysql - 如何添加警报 'if data parameter = nil'

ios - 快速导航 View Controller 。 UIBarButtonItem 没有出现

objective-c - UIScrollView 的最大缩放比例