Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

LiveQueryErrors.InvalidJSONError #43

Closed
JustinDMoore opened this issue Jun 17, 2016 · 1 comment
Closed

LiveQueryErrors.InvalidJSONError #43

JustinDMoore opened this issue Jun 17, 2016 · 1 comment

Comments

@JustinDMoore
Copy link

JustinDMoore commented Jun 17, 2016

Running latest versions of parse server and iOS SDK.

Server setup:

  liveQuery: {
    classNames: ["ChatRooms", "ChatMessages"]
  }

ChatRooms.swift

    let liveQueryClient = ParseLiveQuery.Client()
    private var subscription: Subscription<PChatRoom>?

    var chatRoomsQuery: PFQuery {
        if isPrivate {
            return (PChatRoom.query()?
                .whereKey("name", containsString: PUser.currentUser()?.objectId)
                .whereKey("private", equalTo: true)
                .whereKey("belongsToUser", equalTo: PUser.currentUser()!)
                .orderByDescending("createdAt"))!
        } else {
            return (PChatRoom.query()?
                .whereKey("isPrivate", equalTo: false)
                .orderByDescending("createdAt"))!
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        getExistingRooms()
        subscribeToUpdates()
    }

    func subscribeToUpdates() {
          subscription = liveQueryClient
            .subscribe(chatRoomsQuery)
            .handle(Event.Created) { _, room in
                self.addRoom(room)
        }
    }

    func getExistingRooms() {
        chatRoomsQuery.findObjectsInBackground().continueWithBlock() { task in
            if let chatRooms = task.result as? [PChatRoom] {
                for room in chatRooms {
                    self.arrayOfChatRooms.append(room)
                }
                self.tableView.reloadData()
            }
            return nil
        }
    }

Messages.swift

    let liveQueryClient = ParseLiveQuery.Client()
    private var subscription: Subscription<PChatMessage>?

    var chatQuery: PFQuery {
        if isPrivate {
            return (PChatMessage.query()?
                .whereKey("room", equalTo: room)
                .whereKey("private", equalTo: true)
                .whereKey("belongsToUser", equalTo: PUser.currentUser()!)
                .orderByDescending("createdAt"))!
        } else {
            return (PChatMessage.query()?
                .whereKey("isPrivate", equalTo: false)
                .whereKey("room", equalTo: room)
                .orderByDescending("createdAt"))!
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        getExistingMessages()
        subscribeToUpdates()
    }

    func getExistingMessages() {
        chatQuery.findObjectsInBackground().continueWithBlock() { task in
            if let chatLog = task.result as? [PChatMessage] {
                for message in chatLog {
                    self.addMessage(message.author.objectId!, text: message.message)
                }
                self.finishReceivingMessage()
            }
            return nil
        }
    }

    func subscribeToUpdates() {
        subscription = liveQueryClient
            .subscribe(chatQuery)
            .handle(Event.Created) { _, message in
                self.incomingMessage(message)
        }
    }

Nearly identical liveQueries in both classes.
ChatRooms.swift works fine and receives live updates.
When calling subscribe in Messages.swift, the console outputs only "140725914386528"
and breakpoints at throw LiveQueryErrors.InvalidJSONError(json: json, expectedKey: key) in Operation.swift

        func jsonValue<T>(json: [String:AnyObject], _ key: String) throws -> T {
            guard let value =  json[key] as? T
                else {
                    **throw LiveQueryErrors.InvalidJSONError(json: json, expectedKey: key)**
            }
            return value
        }

What might be causing this to be thrown?

@alecsechechel
Copy link

Hi,

I have same problem when use conditions with Date or Location. I think they not support. In you case I think you have error because use this conditions ".whereKey("belongsToUser", equalTo: PUser.currentUser()!) "
Please check it

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants