ParseLiveQuery无法访问Pointer对象

时间:2016-11-25 12:09:16

标签: swift parse-platform parse-server pfquery back4app

您好我正在尝试通过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 link branch->fix-object-decode

我测试了“fix-object-decode”分支。

它返回格式良好的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',原因:'Key“用户名”没有数据。在获取其值之前调用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
}

现在它工作正常但是...系统说“警告:正在主线程上执行长时间运行的操作。”

我认为这不是最好的..

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我注意到解析服务器不支持includekey。