我正在尝试传播一个 PFObject,其中包含指向PFUser('所有者')的指针:
这是我的获取代码:
public func collectHashTags(#sender:UIViewController, withinGeoCoordinate geoPoint:PFGeoPoint) {
var query = PFQuery(className:"Tag")
query.includeKey("owner") //...note: this doesn't give me the owner object.
// query.whereKey("lastGeo", nearGeoPoint:geoPoint, withinKilometers:2)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let tagArray = query.findObjects() as [PFObject]
if tagArray.count > 0 {
for x in 0...(tagArray.count - 1) {
disseminateTags(tag:tagArray[x]) // ...load global Tag Array.
}
} else {
showAlert(sender, withTitle:"No Data", withMessage:"The HashTag file is empty.")
}
});
}
下面是“标签”,一个 PFObject 包含我想要获取的数据:
private func disseminateTags(#tag:PFObject) {
let owner:PFUser = tag[gParseOwner] as PFUser
let parseID = tag.objectId as String
let displayName = tag[gParseDisplayName] as String
let description = tag[gParseDescription] as String
let radius = tag[gParseRadius] as CGFloat
let tagPublicFlag = tag[gParsePublic] as Bool
let startTime = tag[gParseStartTime] as NSDate
let endTime = tag[gParseEndTime] as NSDate
let media = tag[gParseMedia]? as [Int]
let startTimeString:String = startTime.toString()
let endTimeString:String = endTime.toString()
// Create the local hashTag object:
let myTag = hashTag(parseID: parseID, name: displayName, havingDescription: description, owner: owner)
myTag.radius = radius
myTag.publicFlag = tagPublicFlag
myTag.startTimeStamp = startTimeString
myTag.endTimeStamp = endTimeString
gTagArray.append(myTag)
}
如您所见,“标签”有数据。我想传播“所有者”,一个 PFUser 对象:
(lldb) po tag
<Tag: 0x7fac1f91c950, objectId: 5J1dNXUdaM, localId: (null)> {
Media = (
3,
2,
1
);
accessPemission = 1;
description = "Hashbrown Potatoes";
displayName = Smirf123;
endTime = "2104-12-17 20:20:00 +0000";
geoPoint = "<PFGeoPoint: 0x7fac1f99a990, latitude: 40.000000, longitude: -30.000000>";
owner = "<PFUser: 0x7fac1f993370, objectId: hk7zYRK4xB>";
public = 1;
radius = 2000;
startTime = "2104-12-15 20:20:00 +0000";
}
但我遇到了一个问题。显然我得到了“所有者”(注意它的objectID,见上文),但在调试器中看不到它:
第二次尝试:
使用 'includeKey("owner") 对我没有帮助。所以我把它注释掉了,所以“所有者”属性与它的兄弟姐妹一起包含在“标签”类中。
然后我尝试对所有者(id)字段进行查询(参考:https ://www.parse.com/docs/ios_guide#queries/iOS ):
public func collectHashTags(#sender:UIViewController, withinGeoCoordinate geoPoint:PFGeoPoint) {
var query = PFQuery(className:"Tag")
// query.includeKey("owner")
// query.whereKey("lastGeo", nearGeoPoint:geoPoint, withinKilometers:2)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let tagArray = query.findObjects() as [PFObject]
if tagArray.count > 0 {
for x in 0...(tagArray.count - 1) {
disseminateTags(tag:tagArray[x]) // ...load global Tag Array.
}
} else {
showAlert(sender, withTitle:"No Data", withMessage:"The HashTag file is empty.")
}
});
}
private func disseminateTags(#tag:PFObject) {
let owner = tag[gParseOwner] as PFUser
owner.fetchIfNeededInBackgroundWithBlock {
(object: PFObject!, error: NSError!) -> Void in
if error != nil {
println("Error: \(error)")
} else {
println("Fetched Owner: \(object)")
}
}
}
这次 NSError 用以下标记:
...Error: object not found for get (Code: 101, Version: 1.5.0)
...Error: object not found for get (Code: 101, Version: 1.5.0)
...Error: object not found for get (Code: 101, Version: 1.5.0)
...Error: object not found for get (Code: 101, Version: 1.5.0)
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d260af40 {error=object not found for get, code=101}
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d410ee80 {error=object not found for get, code=101}
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d410ee20 {error=object not found for get, code=101}
Error: Error Domain=Parse Code=101 "The operation couldn’t be completed. (Parse error 101.)" UserInfo=0x7ff9d41082a0 {error=object not found for get, code=101}
第三次尝试(使用后续的 fetchIfNeeded()):
public func collectHashTags(#sender:UIViewController, withinGeoCoordinate geoPoint:PFGeoPoint) {
var query = PFQuery(className:"Tag")
// query.whereKey("lastGeo", nearGeoPoint:geoPoint, withinKilometers:2)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let tagArray = query.findObjects() as [PFObject]?
if tagArray != nil {
let myTag = tagArray![0]
println("***myTag: \(myTag)")
let myName = myTag["displayName"] as String
println("displayName: \(myName)")
let myOwner = myTag["owner"] as PFUser
myOwner.fetchIfNeeded()
println("**** myOwner: \(myOwner)")
}
});
}
输出:
***myTag: <Tag: 0x7fa5d87e8f70, objectId: 5J1dNXUdaM, localId: (null)> {
Media = (
3,
2,
1
);
accessPemission = 1;
description = "Hashbrown Potatoes";
displayName = Smirf123;
endTime = "2104-12-17 20:20:00 +0000";
geoPoint = "<PFGeoPoint: 0x7fa5de104770, latitude: 40.000000, longitude: -30.000000>";
owner = "<PFUser: 0x7fa5de108070, objectId: hk7zYRK4xB>";
public = 1;
radius = 2000;
startTime = "2104-12-15 20:20:00 +0000";
}
displayName: Smirf123
2014-12-14 15:09:12.004 Bliss2[4757:181319] Error: object not found for get (Code: 101, Version: 1.5.0)
**** myOwner: <PFUser: 0x7fa5de108070, objectId: hk7zYRK4xB, localId: (null)> {
}
我在这里迷路了。
使用托管记录传播 PFUser 指针应该不会那么困难。
如何从“所有者”(PFUser)获取/提取(字符串和图像)属性?