Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift项目中使用iOS SDK插入firebase服务器时间戳_Ios_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

如何在Swift项目中使用iOS SDK插入firebase服务器时间戳

如何在Swift项目中使用iOS SDK插入firebase服务器时间戳,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,在中,它提到了kFirebaseServerValueTimestamp,这在Swift项目中似乎不起作用(自动完成不会提示它) 我尝试了[“.sv”:“timestamp”],就像Swift示例那样。但它抱怨说,这不能与其他值一起设置,必须是给定路径上的唯一值 我试过: ref.childByAutoId().setValue(["text": "foobar", ".sv": "timestamp"], withCompletionBlock: {...} 它给了我一个错误,.sv不能与

在中,它提到了
kFirebaseServerValueTimestamp
,这在Swift项目中似乎不起作用(自动完成不会提示它)

我尝试了
[“.sv”:“timestamp”]
,就像Swift示例那样。但它抱怨说,这不能与其他值一起设置,必须是给定路径上的唯一值

我试过:

ref.childByAutoId().setValue(["text": "foobar", ".sv": "timestamp"], withCompletionBlock: {...} 
它给了我一个错误,.sv不能与其他值一起保存

我想做的是保存createdAt时间戳以及对象的其他值。我更喜欢使用服务器时间来避免客户端时钟偏移

我该怎么做?

“.sv”:“timestamp”
添加到
Firebase
条目时,将其添加到括号
[]
之间

例如:


从Firebase 4.0开始,您可以使用ServerValue.timestamp()

例如:

    let ref = Database.database().reference().child("userExample")
    let values = ["firstName": "Joe", "lastName": "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
        }

    }

是否可以尝试仅设置.sv值,并使用{}而不是[]?或者把它放在自定变量而不是.sv中?文档中引用了什么?谢谢-这非常有用-他们应该把它加回去
date: 1463329843759
    let ref = Database.database().reference().child("userExample")
    let values = ["firstName": "Joe", "lastName": "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
        }

    }