解析 PFQuery returns 无 (swift)
Parse PFQuery returns nil (swift)
我正在尝试将我创建的名为 "Party" 的 class 数组添加到名为 "parties" 的数组中,以便在 UITableView 中使用,但是我创建的函数 returns数组的 nil 值,即使在 Parse 中有一个。
我在我的视图控制器中创建了这样的数组:
private var parties:[Party] = [Party]()
我也试过同样的结果:
var parties:[Party] = [Party]()
函数如下所示:
func retrieveParties() {
//create new PFQuery
var query:PFQuery = PFQuery(className: "Party")
//call findobjectinbackground
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
//clear parties
self.parties = [Party]()
//loop through the objects array
for partyObject in objects! {
//retrive the Text column value for each PFobject
let party:Party? = (PFObject: partyObject as? Party)
//Assign it to our parties
if party != nil{
self.parties.append(party!)
}
}
dispatch_async(dispatch_get_main_queue()){
//reload the table view
self.discoverTableView.reloadData()
}
}
}
我也试过这个:
func getParties() {
// Clear up the array
parties.removeAll(keepCapacity: true)
self.discoverTableView.reloadData()
// Pull data from Parse
let query = PFQuery(className:"Party")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for (index, object) in enumerate(objects) {
// Convert PFObject into Party object
let party = Party(pfObject: object)
self.parties.append(party)
}
}
} else {
// Log details of the failure
println("Error: \(error) \(error!.userInfo)")
}
}
//query.cachePolicy = PFCachePolicy.NetworkElseCache
}
无论我使用哪个函数,结果数组都是空的。非常感谢任何能解决这个问题的人!
哦,我刚刚解决了这个问题!我所做的只是将 getParties() 函数更改为:
func getParties() {
// Clear up the array
parties.removeAll(keepCapacity: true)
self.discoverTableView.reloadData()
// Pull data from Parse
let query = PFQuery(className:"Party")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for (index, object) in enumerate(objects) {
// Convert PFObject into Party object
let party = Party(pfObject: object)
self.parties.append(party)
}
}
} else {
// Log details of the failure
println("Error: \(error) \(error!.userInfo)")
}
dispatch_async(dispatch_get_main_queue()){
//reload the table view
self.discoverTableView.reloadData()
query.cachePolicy = PFCachePolicy.NetworkElseCache
}
}
//query.cachePolicy = PFCachePolicy.NetworkElseCache
}
它神奇地起作用了!希望这个解决方案对遇到这个问题的其他人有用:)
我正在尝试将我创建的名为 "Party" 的 class 数组添加到名为 "parties" 的数组中,以便在 UITableView 中使用,但是我创建的函数 returns数组的 nil 值,即使在 Parse 中有一个。
我在我的视图控制器中创建了这样的数组:
private var parties:[Party] = [Party]()
我也试过同样的结果:
var parties:[Party] = [Party]()
函数如下所示:
func retrieveParties() {
//create new PFQuery
var query:PFQuery = PFQuery(className: "Party")
//call findobjectinbackground
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
//clear parties
self.parties = [Party]()
//loop through the objects array
for partyObject in objects! {
//retrive the Text column value for each PFobject
let party:Party? = (PFObject: partyObject as? Party)
//Assign it to our parties
if party != nil{
self.parties.append(party!)
}
}
dispatch_async(dispatch_get_main_queue()){
//reload the table view
self.discoverTableView.reloadData()
}
}
}
我也试过这个:
func getParties() {
// Clear up the array
parties.removeAll(keepCapacity: true)
self.discoverTableView.reloadData()
// Pull data from Parse
let query = PFQuery(className:"Party")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for (index, object) in enumerate(objects) {
// Convert PFObject into Party object
let party = Party(pfObject: object)
self.parties.append(party)
}
}
} else {
// Log details of the failure
println("Error: \(error) \(error!.userInfo)")
}
}
//query.cachePolicy = PFCachePolicy.NetworkElseCache
}
无论我使用哪个函数,结果数组都是空的。非常感谢任何能解决这个问题的人!
哦,我刚刚解决了这个问题!我所做的只是将 getParties() 函数更改为:
func getParties() {
// Clear up the array
parties.removeAll(keepCapacity: true)
self.discoverTableView.reloadData()
// Pull data from Parse
let query = PFQuery(className:"Party")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for (index, object) in enumerate(objects) {
// Convert PFObject into Party object
let party = Party(pfObject: object)
self.parties.append(party)
}
}
} else {
// Log details of the failure
println("Error: \(error) \(error!.userInfo)")
}
dispatch_async(dispatch_get_main_queue()){
//reload the table view
self.discoverTableView.reloadData()
query.cachePolicy = PFCachePolicy.NetworkElseCache
}
}
//query.cachePolicy = PFCachePolicy.NetworkElseCache
}
它神奇地起作用了!希望这个解决方案对遇到这个问题的其他人有用:)