您好我正在尝试通过 ParseLiveQuery 获取我的对象
我认为 ParseLiveQuery 不支持指针对象。
这是我的代码片段。
Post.swift
import Foundation
import Parse
class Post: PFObject, PFSubclassing {
@NSManaged var postBy: PFUser?
@NSManaged var postText: String?
@NSManaged var postImg: PFFile?
static func parseClassName() -> String {
return "Post"
}
override class func query() -> PFQuery<PFObject>? {
//1
let query = PFQuery(className: Post.parseClassName())
query.whereKeyExists("objectId")
//2
query.includeKeys(["postBy"])
//3
query.order(byDescending: "createdAt")
return query
}
}
extension Post: FeedCellSupport {
var username:String?{
return postBy?["username"] as? String
}
var userImg:PFFile?{
return postBy?["profileImg"] as? PFFile
}
FeedVC.swif
let liveQueryClient = ParseLiveQuery.Client()
protocol FeedCellSupport {
var username:String?{ get }
var userImg:PFFile?{ get }
var postText: String? { get }
}
class FeedVC: UITableViewController, ShopDetailDelegate {
private var subscription: Subscription<Post>!
//Add friends and me
var postLiveQuery: PFQuery<Post> {
return (Post.query()?
.whereKeyExists("objectId")
.includeKeys(["postBy", "commentBy", "commentBy.commentBy"])
.order(byAscending: "createdAt")) as! PFQuery<Post>
}
var results = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
subscription = liveQueryClient.subscribe(postLiveQuery).handleSubscribe { [weak self] (_) in
// Fetch the objects on subscription to not miss any
self?.fetchObjects()
}.handleEvent { [weak self] (_, event) in
self?.handleEvent(event: event)
}
}
我有 2 个问题。
1. ParseLiveQuery 已像 REST 结果一样返回 PFObject。
当我第一次获取对象时它工作正常。
我的意思是我可以通过 findObjectBackground 获取用户名和 userImg 数据。
<Post: 0x6180000b43a0, objectId: w5qcfMCByF, localId: (null)> {
likeCount = 0;
postBy = "<PFUser: 0x6180000f4600, objectId: rgG7XfAYWj, localId: (null)>";
postImg = "<PFFile: 0x618000246ba0>";
postText = nice2MeetYous121;
sell = 0;
}
然后收到更新的事件后..我得到了一个不同风格的PFObject。
<Post: 0x6100000b3020, objectId: w5qcfMCByF, localId: (null)> {
"__type" = Object;
className = Post;
createdAt = "2016-11-19T12:37:30.896Z";
likeCount = 0;
postBy = {
"__type" = Pointer;
className = "_User";
objectId = rgG7XfAYWj;
};
postImg = {
"__type" = File;
name = "92aa66bfdcf5f70d1d277556bbd9d7ca_post.jpg";
url = "https://parsefiles.back4app.com/PlY72cbRxsOIXQ1qbJjaQtudZQU9HA8U2RIg2oE1/92aa66bfdcf5f70d1d277556bbd9d7ca_post.jpg";
};
postText = nice2MeetYous1211;
sell = 0;
updatedAt = "2016-11-21T14:34:11.338Z";
}
所以我收到一条错误消息,因为 Parse LiveQuery 像 REST 一样返回。
2. 当我尝试过“fix-object-decode”分支时,它返回 PFObject 而不是 REST 样式。但它也不能直接检索我的指针数据。
ios ParseLiveQuery 链接分支->修复对象解码
我已经测试了“修复对象解码”分支。
它返回格式良好的 PFObject,但结果似乎不同。
当我通过 findObjectInBackGround() 请求时,这是我原来的 PFObject
<Post: 0x6080000b0800, objectId: w5qcfMCByF, localId: (null)> {
likeCount = 0;
postBy = "<PFUser: 0x6080000e3100, objectId: rgG7XfAYWj, localId: (null)>";
postImg = "<PFFile: 0x60800024c150>";
postText = nice2MeetYous1211;
sell = 0;
}
当我更改 postText 时,我通过实时查询获得了更新事件,但有所不同。
<Post: 0x6100000b1be0, objectId: w5qcfMCByF, localId: (null)> {
likeCount = 0;
postBy = "<PFUser: 0x6100000e6900, objectId: rgG7XfAYWj, localId: (null)>";
postImg = "<PFFile: 0x61000025d1f0>";
postText = nice2MeetYous;
sell = 0;
}
如您所见..PFUser:0x6080000e3100 和 PFFile:0x60800024c150 已更改为 PFUser:0x6100000e6900,PFFile:0x61000025d1f0
无论如何,当我收到事件时,我无法获取对象。
这个问题是关于实时查询还是解析服务器端的问题?
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“键“用户名”没有数据。在获取其值之前调用 fetchIfNeeded。
我添加了一些代码以避免 fetchIfNeeded 错误。
这是我的代码段。
extension Post: FeedCellSupport {
var username:String?{
do {
try postBy?.fetchIfNeeded()
} catch _ {
print("There was an error")
}
return postBy?["username"] as? String
}
var userImg:PFFile?{
do {
try postBy?.fetchIfNeeded()
} catch _ {
print("There was an error")
}
return postBy?["profileImg"] as? PFFile
}
现在它工作正常但是...系统说“警告:正在主线程上执行长时间运行的操作。”
我认为这不是最好的方式..
你能帮我吗?