swift数组在循环时不附加

时间:2016-10-12 06:55:46

标签: ios arrays swift loops parse-platform

我正在尝试从Parse检索数据以追加到我的数组:

var displayedGroups = [Groups]()

群组是一个结构:

struct Groups{
    let name: String?
    let date: Date
    let number: Int
    let id: String

    init(id: String, name: String, date: Date, number: Int) {
        self.id = id
        self.name = name
        self.date = date
        self.number = number
    }
}

这是我尝试使用以下函数检索数据的函数:

func getDisplayedGroups() {
        let query = PFQuery(className: "MeetUps")
        print(PFUser.current()?.username)
        query.whereKey("peeps", contains: PFUser.current()?.username)
        query.findObjectsInBackground { (objects, error) in
            if error != nil {
                print(error)
            } else if let objects = objects {
                for meetUps in objects {
                    let peeps = (meetUps["peeps"] as! [String])
                    let id = meetUps.objectId!
                    //let name = meetUps["meetUpName"] as? String
                    let date = meetUps["when"] as! Date
                    let group = Groups(id: id, name: " ", date: date, number: peeps.count)
                    print(group)
                    self.displayedGroups.append(group)
                }
            }
        }
        for i in displayedGroups {
            print(i)
        }
        tableView.reloadData()
    }

我已注释掉let name = meetUps["meetUpName"] ...因为我的解析仪表板的“meetUpName”-key列中有nil值。

print(group)工作,它为每个循环打印一个不同的组。

然而,这个块:

for i in displayedGroups {
                print(i)
            }

根本不打印:/因此我的表没有从shownGroups加载数据。它与异步有关吗?应该不对?因为他们在同一个街区。我不知道,请指教。谢谢!

2 个答案:

答案 0 :(得分:1)

findObjectsInBackground异步工作,将循环和行重新加载到完成块中的表格视图

您可能需要在主线程上重新加载表视图。

...
    for meetUps in objects {
      let peeps = (meetUps["peeps"] as! [String])
      let id = meetUps.objectId!
      //let name = meetUps["meetUpName"] as? String
      let date = meetUps["when"] as! Date
      let group = Groups(id: id, name: " ", date: date, number: peeps.count)
      // print(group)
      self.displayedGroups.append(group)
    }
    for i in displayedGroups {
      print(i)
    }
    dispatch_async(dispatch_get_main_queue) {
       self.tableView.reloadData()
    }
...

答案 1 :(得分:0)

从代码中可以看出,findObjectsInBackground在后台运行,这意味着它运行在不同的线程而不是主线程中。所以下面的行将被执行

for i in displayedGroups {
        print(i)
    }
    tableView.reloadData()

在完成后台操作之前。

请将上述代码移到完成块内,不要忘记从主线程重新加载数据。

编辑功能:

func getDisplayedGroups() {
let query = PFQuery(className: "MeetUps")
print(PFUser.current()?.username)
query.whereKey("peeps", contains: PFUser.current()?.username)

query.findObjectsInBackground { (objects, error) in
    if error != nil {
        print(error)
    } else if let objects = objects {
        for meetUps in objects {
            let peeps = (meetUps["peeps"] as! [String])
            let id = meetUps.objectId!
            //let name = meetUps["meetUpName"] as? String
            let date = meetUps["when"] as! Date
            let group = Groups(id: id, name: " ", date: date, number: peeps.count)
            print(group)
            self.displayedGroups.append(group)
        }
    }
    for i in self.displayedGroups {
        print(i)
    }
    dispatch_async(dispatch_get_main_queue()) {

    tableView.reloadData()
    }
}
}
}