ParseSwift Query Question

Hello,

Would someone here be able to show me, or point me, to a sample to query for a user? I have a collection that has a column with a user pointer. I am able to create queries for strings, int, etc. But I am unclear as to the syntax for finding a record that matches a user pointer.

U. hope this makes sense, I have used ParseiOS for a long time and am familiar, but I am new to SwiftUI and ParseSwift. There are not many samples and not much doc so I am struggling with it a bit.

Thanks for any help.

The playgrounds is the best place for examples:

In terms of querying for pointers, querying on the User class is the same as any other Parse class:

If you want to use async/await, just use try await instead of try when querying. More on async/await here:

A complete app showing how to use the Swift SDK in SwiftUI is here:

96% of the SDK is documented: ParseSwift Reference

1 Like

Ok, yes, SnapCat was very helpful, I got qeuries working, but I do have one thing I do not understand.

I can do this: Word.query(WordKey.word == “Some Text”) and it works (string compare)
I can so this: Word.query(WordKey.wordOrder < 8) and it works (int)
But this does not: Word.query(WordKey.isWordNeeded == true) Bool fields do not seem to work, it always return a 0 result set

Is there something special with a bool that I am missing? SnapCat does not query on bools anywhere and the doc part showing comparison functions does not having anything special for bools

What version of the Parse-Server and Swift SDK are you using?

Your bool query is essentially using:

Which should work. It’s possible the server isn’t like how this is being encoded in JSON. Or, the server can have a bug, but this seems like a simple query that should be covered in the server tests.

I think Parse-Server is the latest and my project is Swift 5 Xcode 13.1

Ok, so I checked my code and it seems correct, I will verify which Parse-Server version I am running, but I agree this should be covered as it is pretty basic.

Can you open an issue on the Swift SDK? I’m guessing this is related to:

Basically, $eq was removed because of the way LiveQuery used it. It’s possible $eq is needed for regular queries and can’t leverage the same way LiveQuery uses it. If this is the case, it’s problematic as the server should choose a specific way or handle both cases.

@davimacedo @dplewis @Manuel if my guess is correct, this is also related to my comments on:

Also related to:

Hmmm, ok, I will post the same thing I posted here on the Swift SDK and see where it goes.

Ok, posted here: Bool query not working... · Issue #282 · parse-community/Parse-Swift · GitHub

I think this is a false alarm. I checked a bool query in Swift Playgrounds and it works fine as is.

@LilMoke I recommend you check your code and parse dashboard for mistakes. You can test the example I used in Swift Playgrounds here (directions in the README about how to use docker to get a test server up and running in seconds):

Hmmmm, ok, I will check it out, thank you for looking further! It could very well be me, a lot of new things for me with SwiftUI and ParseSwift… lol

cbaker6, so sorry on the bool, it was my fault, cs101 mistake, but I do have one more question.

I am doing this:

@ObservedObject var wordsWordViewModel = Word.query(WordKey.isActiveButton == true,
												WordKey.cgsUser == User.current)
	.includeAll()
	.order([.ascending(WordKey.buttonOrder)])
	.viewModel

The WordKey.cgsUser is a pointer to a user in the User collection. If I remove that line the bool works, I fixed that issue, but now when I add the pointer to user so I can pull only the records for that user, it does not match. I added .includeAll so it would include the User collection.

What am I doing wrong with this one?

However, on a good note, I have a better understanding of it now, so hopefully the pointer matches are my last crazy questions.

Thanks for the help!!

There needs to be a try? as the Query will fail User.current can’t be converted to a Pointer, or you can convert User.current to a pointer before using it in a query, Functions Reference

Your view model is getting kind of long, you may want to setup your view model in an init, like I did here:

I used a similar query here:

The Post model where user is a ParseUser (essentially a Pointer) is here:

cbaker6, can you briefly explain to me why in SnapCat you set the default calc for all parse objects? I have been using Parse for several UIKit apps over the past few years and have never had to do that.

  1. Is that new for ParseSwift?
  2. Is there an advantage to doing that?
  3. Is it a security reason?
  4. Is there some feature SnapCat has that requires you to do that?

Thank you, BTW, your help above has helped me with loading pointer objects. Lol, I am starting to become a SnapCat lover and expert

Are you referring to the defaultACL? If so, I believe a few of the SDK’s have this, particularly the Obj-C SDK, iOS Developers Guide | Parse.

To make it super easy to create user-private ACLs for every object, we have a way to set a default ACL that will be used for every new object you create

This is useful if you are creating ParseObject classes from the client side. Many developers use Cloud Code to manage ACL’s and CLP’s; you can also use the Parse Dashboard. In SnapCat, this is done on each ParseObject, because the defaultACL currently isn’t set to the object before a save. I may add this if I get some time later this week.

Thank you, BTW, your help above has helped me with loading pointer objects. Lol, I am starting to become a SnapCat lover and expert

This is great to hear! I’m glad SnapCat is useful. If you find any bugs, feel free to open a PR!

Why do some places you use QueryViewModel and other places, for example, you use TimeLineViewModel, etc. Why do you need QueryViewModel? And, likewise, why can’t you always use classes like TimeLineViewModel… if the question makes sense.

Is it because QueryViewModel is a ParseObject? It seems like TimeLineViewModel and others can be QueryViewModels

Oh wait, I am sorry, I see it know, QueryViewModel us part of ParseSwift. And things like TimeLineViewmodel are your own View Model Classes. And QueryViewModel has some useuful comments with explanation

Yes, those comments make up the documentation here: QueryViewModel Class Reference

To see how to combine a standard query with live query to make a powerful view model that reactively responds to changes on the server, you can look at QueryImageViewModel which subclasses SubscriptionQueryViewModel. This is used whenever you see the property subscribeCustom.

Ahhh ok, yes, I did see that. Thank you. So why does your ProfileView user QueryViewModel and your ActivityView uses ActivityViewModel. Couldn’t ProfileView just use a ProfileViewModel and all the queries required live there? I guess I don’t get why the ProfileView need QueryViewModel?

It matters what you need for your views. In my case, I’m tying multiple view models to ProfileView (and other views). Some of the view models are simple and can use the Swift SDK stock view model, others are more complicated. Sometimes I want my view models to be reactive using LiveQuery, other times the views can just use standard queries. In the end, it’s about your use-cases and how you choose to design using MVVM (or variations of it).

Ok, that is exactly what I was thinking but I was not sure. It was good to here you validate what I was thinking. That was how it appeared, but I was not entirely sure.