Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
Firebase在优先顺序列表swift的一部分上更新查询_Swift_Firebase_Swift2_Firebase Realtime Database - Fatal编程技术网

Firebase在优先顺序列表swift的一部分上更新查询

Firebase在优先顺序列表swift的一部分上更新查询,swift,firebase,swift2,firebase-realtime-database,Swift,Firebase,Swift2,Firebase Realtime Database,我有一个收集视图,用户可以在其中添加照片,也可以更改照片的顺序。当他们将照片添加到视图中时,我根据collectionViewImageSource.count分配优先级值,基本上将其作为最后一张图像。然而,我注意到,在重新排序一些照片,然后重新加载应用程序以查询按优先级排序的图像节点后,这就是我返回的每个图像的优先级 (一) (1) (2) (4) (4) (5) (8) (8) (10) (12) (13) (14) 当用户将照片移动到另一个索引路径时,我将使用setPriority在fir

我有一个收集视图,用户可以在其中添加照片,也可以更改照片的顺序。当他们将照片添加到视图中时,我根据collectionViewImageSource.count分配优先级值,基本上将其作为最后一张图像。然而,我注意到,在重新排序一些照片,然后重新加载应用程序以查询按优先级排序的图像节点后,这就是我返回的每个图像的优先级

(一) (1) (2) (4) (4) (5) (8) (8) (10) (12) (13) (14)

当用户将照片移动到另一个索引路径时,我将使用setPriority在firebase中切换移动图像的优先级,但显然,未被触摸的图像保持相同的优先级,因此您开始获得副本

当一次仅更改两个图像优先级时,重新安排图像优先级节点的最佳方法是什么

编辑:

目前,我使用for-in循环遍历所有图像,并使用递增计数器重置它们的优先级。这至少让我每次都能以正确的顺序检索它们,但如果有更好的方法,请让我知道

嘿Jay我有UserImage结构

struct UserImage {

let key: String
let imageURL: String
var priority: Int?

init(key: String, imageURL: String, priority: Int?) {
    self.key = key
    self.imageURL = imageURL
    self.priority = priority
}

init(snapshot: FIRDataSnapshot) {
    key = snapshot.key
    imageURL = snapshot.value as! String
    priority = snapshot.priority as? Int
}
我有一个networking类,它有一个生成所有当前用户图像的函数,我按优先级从firebase中提取图像,并创建一个userImages数组,在完成块中返回

func generateUserImages(completion: [UserImage]? -> Void) {
    guard let userId = FIRAuth.auth()?.currentUser?.uid else { return }
    let userRef = FirebaseData.fbData.REF_USERS.child(userId)


    userRef.child(Child.Images.camelCaseString()).queryOrderedByPriority().observeSingleEventOfType(.Value, withBlock: { snapshot in
        if snapshot.exists() {
            var userPhotoArray = [UserImage]()
            let snapChildren = snapshot.valueInExportFormat() as! NSDictionary
            for snap in snapChildren {


                guard let imageUrl = snap.value[".value"] as? String, priority = snap.value[".priority"] as? Int, key = snap.key as? String else { return }
                let userImage = UserImage(key: key, imageURL: imageUrl, priority: priority)
                userPhotoArray.append(userImage)
                }
            var sortedArray = userPhotoArray
            sortedArray.sortInPlace({ $0.priority < $1.priority })
            print(sortedArray.count)
            completion(sortedArray)

        } else {
           completion(nil)
           print("NO USER IMAGES")
        }

    })

}

我将ImageURL存储在firebase数据库中,并将图像存储在存储器中。我用Alamofire从数据库中获取图像。我正在使用ChildByAutoId存储图像。

图像是存储在Firebase数据库中还是存储在存储器中?你能把你的数据结构的一个片段作为文本而不是图像发布吗?(Firebase控制台->右侧三点->导出JSON。另外,图像索引是否以代码或其他方法存储在数组中?
alert.addAction(UIAlertAction(title: "Make Primary Photo", style: .Default, handler: { alertAction in
                let newPrimaryImageIndexPath = indexPath
                let oldPrimaryImageIndexPath = NSIndexPath(forItem: 0, inSection: 0)
                let indexPath1 = NSIndexPath(forItem: 1, inSection: 0)
                let newPrimaryImage = self.currentUserImages[indexPath.row]
                let oldPrimaryImage = self.currentUserImages[0]
                self.currentUserImages.removeAtIndex(indexPath.row)
                self.currentUserImages.insert(newPrimaryImage, atIndex: 0)
                self.currentUserImages.removeAtIndex(1)
                self.currentUserImages.insert(oldPrimaryImage, atIndex: indexPath.row)
                collectionView.moveItemAtIndexPath(newPrimaryImageIndexPath, toIndexPath: oldPrimaryImageIndexPath)
                collectionView.moveItemAtIndexPath(indexPath1, toIndexPath: newPrimaryImageIndexPath)
                var counter = 1
                for image in self.currentUserImages {
                    self.currentUserRef.child(Child.Images.camelCaseString()).child(image.key).setPriority(counter)
                    counter += 1
                }