Giter Club home page Giter Club logo

Comments (8)

kuyazee avatar kuyazee commented on August 12, 2024

Have you made any headway on this? I'd like to know I'm having issues with my project right now too

from parselivequery-ios-osx.

flovilmart avatar flovilmart commented on August 12, 2024

It doesn't seem you retain the subscription

from parselivequery-ios-osx.

kuyazee avatar kuyazee commented on August 12, 2024

@flovilmart How do you retain the subscription? I also have a code like this

Here's my model

class Post: PFObject, PFSubclassing {
    @NSManaged var caption: String?
    @NSManaged var type: String?
    
    class func parseClassName() -> String {
        return "Post"
    }
    
    convenience init(_ caption: String) {
        self.init()
        self.caption = caption
        self.type = "normal"
    }
}

Here's my listener code

        let PFLiveQuery: ParseLiveQuery.Client = ParseLiveQuery.Client()

        let postQuery = Post.query()!
        postQuery.whereKey("type", equalTo: "normal")

        // This is just to test if I'm querying anything in the background        
        postQuery.findObjectsInBackground { (ob, er) in
            print(ob, er) // this logs
        }

        let subscription = PFLiveQuery.subscribe(postQuery)
            .handle({ Event.created($0)}, { (query, object) in
                print("PLEASE LOG PLEASE PLEASE LOG") // this doesn't log
            })
            .handleError({ (query, error) in
                print("error happened", query, error) // this doesn't log
            })
            .handleSubscribe({ (query) in
                print("subscribe happened", query) // this doesn't log
            })
            .handleUnsubscribe({ (query) in
                print("unsubscribe happened", query) // this doesn't log
            })
            .handleEvent({ (query, event) in
                switch event {
                case .created(let object):
                    print(object as Any) // this doesn't log
                case .deleted(let object):
                    print(object as Any) // this doesn't log
                case .entered(let object):
                    print(object as Any) // this doesn't log
                case .left(let object):
                    print(object as Any) // this doesn't log
                case .updated(let object):
                    print(object as Any) // this doesn't log
                }
            })
        print(subscription) // this logs

Here is what I'm trying to send

        let post = Post(textField.text!)
        post.saveInBackground { (success, error) in
            if let error = error {
                print("not sent", error) // this logs if there's an error
            } else {
                print("sent") // this logs
            }
        }

I don't know why it doesn't work too

from parselivequery-ios-osx.

flovilmart avatar flovilmart commented on August 12, 2024

Just make it a property in the class that holds the listener code instead of just keeping it a local variable

from parselivequery-ios-osx.

kuyazee avatar kuyazee commented on August 12, 2024

Will that work?

from parselivequery-ios-osx.

kuyazee avatar kuyazee commented on August 12, 2024

Update: I did it like this

class ViewController: UIViewController {
    var btnSend: UIButton?
    var textField: UITextField?

    var subscriber: ParseLiveQuery.Client!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        subscriber = ParseLiveQuery.Client()

        btnSend = view.viewWithTag(12) as! UIButton
        UITextField = view.viewWithTag(13) as! UITextField

        btnSend.addTarget(self, action: #selector(self.sendClicked(sender:)), for: .touchUpInside)
    
        startSocketListener()
    }
    
    func startSocketListener() {
        print("startSocketListener")
        Post.registerSubclass()
        let postQuery = Post.query()!
        postQuery.whereKey("type", equalTo: "normal")
        
//        postQuery.findObjectsInBackground { (ob, er) in
//            print(ob, er)
//        }

        let subscription = subscriber.subscribe(postQuery)
            .handle({ Event.created($0)}, { (query, object) in
                print("PLEASE LOG PLEASE PLEASE LOG")
            })
            .handleError({ (query, error) in
                print("error happened", query, error)
            })
            .handleSubscribe({ (query) in
                print("subscribe happened", query)
            })
            .handleUnsubscribe({ (query) in
                print("unsubscribe happened", query)
            })
            .handleEvent({ (query, event) in
                switch event {
                case .created(let object):
                    print(object as Any)
                case .deleted(let object):
                    print(object as Any)
                case .entered(let object):
                    print(object as Any)
                case .left(let object):
                    print(object as Any)
                case .updated(let object):
                    print(object as Any)
                }
            })
        print(subscription)
    }
    
    @IBAction func tryClicked(_ sender: Any) {
        startSocketListener()
    }
    
    func sendClicked(sender: UIButton) {
        guard !textField.text!.isEmpty else {
            return
        }
        let post = Post(textField.text!)
        post.saveInBackground { (success, error) in
            if let error = error {
                print("not sent", error)
            } else {
                print("sent")
                self.textField.text = ""
            }
        }
    }
}

Still does not work

from parselivequery-ios-osx.

kuyazee avatar kuyazee commented on August 12, 2024

Update: I am 100% sure that my ParseLiveQuery works since my JS code works seamlessly.

var Parse = require('parse/node');


Parse.initialize("....", ".....", "......");
Parse.serverURL = '.....';

var Post = Parse.Object.extend('Post');
var query = new Parse.Query(Post);
query.equalTo('type', 'normal');

var subscription = query.subscribe();

subscription.on('create', function (message) {
 console.log('Message created with text: ' + message.get('caption'));
});

subscription.on('update', function (message) {
 console.log('Message updated with text: ' + message.get('caption'));
});

subscription.on('delete', function (message) {
 console.log('Message deleted with text: ' + message.get('caption'));
 subscription.unsubscribe();
});

console.log('Running');

This works perfectly.

from parselivequery-ios-osx.

kuyazee avatar kuyazee commented on August 12, 2024

Okay I have now made it work. I basically misinterpreted you when you said Just make it a property in the class that holds the listener code instead of just keeping it a local variable cause I only made ParseLiveQuery.Client as the property instead of the Subscription<PFObject>

It works now

from parselivequery-ios-osx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.