我正在创建前十名单。我需要某些项的值来获得一个值,并且每当某个人同意时,该值就会增加。它目前没有更新,但试图添加相同的值并立即消失。这是我的Firebase堆栈....
TopVideos-->
yWfsla_Uh80-->
Value: 1
description: "Download on iTunes : KOR https://itunes.apple.c..."
imageString: "https://i.ytimg.com/vi/yWfsla_Uh80/default.jpg"
rank: 1
title: "EXO_CALL ME BABY_Music Video"
user: "6ef370d4-a1bd-48ce-9ae0-93d2e20f3bc8"
videoID: "yWfsla_Uh80"
我想增加"价值"孩子/节点,但它没有发生。这是我的代码....
topVideoRef.childByAppendingPath("Value").runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? Int
if value == nil {
value = 0
}
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
})
我按照Firebase指南写了这封信,但我可能会遗漏一些东西。顺便说一下......如果你知道答案,请包括如何减少价值。谢谢。
*更新*
我正在创建视频参考.....
vidRank = Int(self.rankValue.text!)!
let vidArray = ["user" : self.ref.authData.uid, "videoID": self.videoId, "rank": self.vidRank, "title": self.vidTitleText, "description": self.vidDescription, "imageString": self.vidIMG]
let topVideoRef = self.ref.childByAppendingPath("TopVideos/\(self.videoId)")
topVideoRef.setValue(vidArray)
答案 0 :(得分:1)
我通过用文本替换字段来修改代码,然后创建了两个函数,链接到窗口上的两个按钮
然后运行代码,它似乎工作。
func clickedButton1() {
print("Button1Action")
let myRootRef = Firebase(url:"https://your-app.firebaseio.com")
let vidArray = ["user" : "some user id",
"videoID": "some video id",
"rank": "some rank",
"title": "a title",
"description": "a description",
"imageString": "an image"]
let topVideoRef = myRootRef.childByAppendingPath("TopVideos/\("vid_id_0")")
topVideoRef.setValue(vidArray)
}
和第二个功能
func clickedButton2() {
print("Button2Action")
let myRootRef = Firebase(url:"https://your-app.firebaseio.com")
let topVideoRef = myRootRef.childByAppendingPath("TopVideos/\("vid_id_0")")
topVideoRef.childByAppendingPath("Value").runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? Int
if value == nil {
value = 0
}
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
})
}
单击按钮1时,将写出初始数据。
每次单击按钮2时,“值”节点将增加1.
我猜你要么在某个时候覆盖整个节点,要么在你退出应用程序时将一个onDisconnect附加到Value上删除它。
或者......它被设置为nil,这将导致你所看到的。它会有一个价值然后突然消失。
答案 1 :(得分:0)
谢谢@Jay的回答。它更好地理解了Firebase数据;检索和存储。我被困了一段时间,因为我得到了我的按钮添加然后更新,但它被连续循环捕获。一旦我使用observeSingleEventofType
,它就会聚集在一起。这是我完成的代码,我最初为用户保存视频,然后检查视频是否为#1视频,并从那里保存或更新...
@IBAction func finalSaveBTN(sender: AnyObject)
{
vidRank = Int(self.rankValue.text!)!
let vidArray = ["user" : self.ref.authData.uid, "videoID": self.videoId, "rank": self.vidRank, "title": self.vidTitleText, "description": self.vidDescription, "imageString": self.vidIMG]
let videoRef = self.ref.childByAppendingPath("UserVideo/\(self.ref.authData.uid)/Vid\(vidRank)")
videoRef.setValue(vidArray)
if self.vidRank == 1
{
let findTopRef = self.ref.childByAppendingPath("TopVideos/\(self.videoId)")
findTopRef.queryLimitedToFirst(1)
.observeSingleEventOfType(.Value, withBlock: { snapshot in
if snapshot.value is NSNull
{
print("Save")
self.clickedButton1()
}
else
{
print("Updated")
self.clickedButton2()
}
})
}
}
func clickedButton1()
{
print("Button1Action")
let vidArray = ["Value" : 1, "videoID": self.videoId, "title": self.vidTitleText, "description": self.vidDescription, "imageString": self.vidIMG]
let topVideoRef = self.ref.childByAppendingPath("TopVideos/\(self.videoId)")
topVideoRef.setValue(vidArray)
}
func clickedButton2()
{
print("Button2Action")
let topVideoRef = self.ref.childByAppendingPath("TopVideos/\(self.videoId)")
topVideoRef.childByAppendingPath("Value").runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? Int
if value == nil {
value = 0
}
currentData.value = value! + 1
return FTransactionResult.successWithValue(currentData)
})
}
如果出现问题,请发表评论。顺便说一句,如果#1 Vid已经存在,我仍然需要降低价值。