How do I get a server timestamp from Firebase's iOS API?

后端 未结 4 656
迷失自我
迷失自我 2020-12-25 15:23

I have an iOS app that uses Firebase and currently has a few dictionaries with keys that are NSDate objects. The obvious issue with this is that NSDate draws from the devic

相关标签:
4条回答
  • 2020-12-25 15:38

    As of Firebase 4.0 you can use ServerValue.timestamp()

    for example:

        let ref = Database.database().reference().child("userExample")
        let values = ["fullName": "Joe Bloggs", "timestamp": ServerValue.timestamp()] as [String : Any]
    
        ref.updateChildValues(values) { (err, ref) in
    
            if let err = err {
                print("failed to upload user data", err)
                return
            }
    
        }
    
    0 讨论(0)
  • 2020-12-25 15:39

    Caveat: Seems like the timestamp is added AFTER your object is persisted in Firebase. This means that if you have a .Value event listener set up on the location your object is persisted to, it will be triggered TWICE. Once for the initial object being stored in the location, and again for the timestamp being added. Struggled with this issue for days :(

    Helpful information for anyone else who can't figure out why their event listeners are triggering twice/multiple times!

    0 讨论(0)
  • 2020-12-25 15:42

    Update: In Firebase 3.0 + Swift, you can use FIRServerValue.timestamp(). In Objective-C this is [FIRServerValue timestamp].

    In Swift, you can now use FirebaseServerValue.timestamp() with Firebase 2.0.3+ (before 3.0).

    The equivalent for Firebase.ServerValue.TIMESTAMP in iOS is kFirebaseServerValueTimestamp. Right now, this only works for Objective-C and not Swift.

    In Swift, you can create your own global timestamp with

    let kFirebaseServerValueTimestamp = [".sv":"timestamp"]
    

    and then you'll be able to use kFirebaseServerValueTimestamp in the same way.

    But you can only use this as the value or priority of a node. You won't be able to set it as the key name (although, I don't believe you could in the Web API either).

    In general, calling allKeys on a dictionary does not guarantee order. But if you're using childByAutoID at a node, you can get back the right order by ordering the NSArray returned by allKeys lexicographically. Something like this would work:

    [ref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
        NSDictionary *value = snapshot.value;
        NSLog(@"Unsorted allKeys: %@", value.allKeys);
        NSArray *sortedAllKeys = [value.allKeys sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"Sorted allKeys: %@", sortedArray);
    }];
    

    This is similar to sorting an NSArray alphabetically, but when sorting the auto-generated IDs, you do not want localized or case insensitive sort, so you use compare: instead of localizedCaseInsensitiveCompare:

    0 讨论(0)
  • 2020-12-25 15:54

    You can get Time Stamp using FIRServerValue.timestamp().

    But, Because of FIRServerValue.timestamp() listener is called two times. Listener will be called two times.

    0 讨论(0)
提交回复
热议问题