0

Hi I'm trying to fetch my object via ParseLiveQuery

I think ParseLiveQuery is not support Pointer Object.

Here is snippet my code.

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)
        }

}

I have a 2 questions.

1. ParseLiveQuery has returned PFObject like an REST results.

When I fetch object first time It works fine.

I mean I can get username and userImg data via findObjectBackground.

<Post: 0x6180000b43a0, objectId: w5qcfMCByF, localId: (null)> {
likeCount = 0;
postBy = "<PFUser: 0x6180000f4600, objectId: rgG7XfAYWj, localId: (null)>";
postImg = "<PFFile: 0x618000246ba0>";
postText = nice2MeetYous121;
sell = 0;
}

After received updated event then..I got an different style 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";
}

So I got an error message because Parse LiveQuery returned like REST.

2. When I had tried branch of "fix-object-decode" then It returned PFObject not REST style. But It also can't retrieve my Pointer Data directly.

ios ParseLiveQuery link branch->fix-object-decode

I had tested "fix-object-decode" branch.

It returned well formatted PFObject but It seems different results.

It is my original PFObject when I request via findObjectInBackGround()

<Post: 0x6080000b0800, objectId: w5qcfMCByF, localId: (null)> {
likeCount = 0;
postBy = "<PFUser: 0x6080000e3100, objectId: rgG7XfAYWj, localId: (null)>";
postImg = "<PFFile: 0x60800024c150>";
postText = nice2MeetYous1211;
sell = 0;
}

When I change postText then I got update event via live query but different.

<Post: 0x6100000b1be0, objectId: w5qcfMCByF, localId: (null)> {
likeCount = 0;
postBy = "<PFUser: 0x6100000e6900, objectId: rgG7XfAYWj, localId: (null)>";
postImg = "<PFFile: 0x61000025d1f0>";
postText = nice2MeetYous;
sell = 0;
}

As you can see..PFUser:0x6080000e3100 and PFFile: 0x60800024c150 has changed to PFUser: 0x6100000e6900, PFFile: 0x61000025d1f0

Anyway I can't fetchObject when I received events.

Is this problem about live query or parse-server side problem?

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Key "username" has no data. Call fetchIfNeeded before getting its value.'

I added some code to avoid fetchIfNeeded errors.

Here is my snippet code.

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
}

Now It works fine but...system says "Warning: A long-running operation is being executed on the main thread. "

I think it is not bestway..

Could you anyone helping me?

Shawn Baek
  • 1,928
  • 3
  • 20
  • 34

1 Answers1

0

I noticed that parse server is not support includekey.

Shawn Baek
  • 1,928
  • 3
  • 20
  • 34
  • update my comment. includeKey is working fine. https://github.com/parse-community/ParseLiveQuery-iOS-OSX/issues/91 – Shawn Baek Sep 09 '17 at 06:53
  • Sure ? https://github.com/parse-community/ParseLiveQuery-iOS-OSX/issues/30 and https://github.com/parse-community/parse-server/issues/1686 – nathan Sep 09 '17 at 14:58
  • Sorry for late. I think it is not working yet. If I solved this problem that I will share in this post. – Shawn Baek Sep 15 '17 at 12:01