f-miyu / Plugin.CloudFirestore

MIT License
121 stars 44 forks source link

GetAsync freezes when grabbing a single document #76

Closed mrobraven closed 3 years ago

mrobraven commented 3 years ago

I began by setting the reference. IDocumentReference postRef = CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24");

And then grab the data and convert to a dictionary IDocumentSnapshot snapshot = postRef.GetAsync(); Dictionary<string, object> list = snapshot.ToObject<Dictionary<string, object>>();

The structure of the data:

Main posts collection with documents (representing each post) including subcollections for reactions and comments as well as the document data that should be returned.

I have also tested to see if the subcollections were causing the issue by creating a post document without them but this is not the issue

posts/

---->$randomID/

--------->comments/ -------------->$randomID/ ------------------->etc...

--------->reactions/ -------------->$randomID/ ------------------->etc....

--------->author = "Matthew" --------->content = "afdgadfgdfgdsfgdf"

Should Return: author = "Matthew" content = "afdgadfgdfgdsfgdf"

Actually Returns: Nothing. Task hangs on the postRef.GetAsync() line and times out with no exceptions.

I have used ICollectionReference.GetAsync() which works and returns a list of all of the documents but I cannot use IDocumentReference.GetAsync() to return a single document.

angelru commented 3 years ago

@mrobraven if comments is a subcollection of posts, to get it you must do something like:

CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").Collection("comments").Document("randomID").GetAsync();

to map to your object:

var document= await CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync();
var post = document.ToObject<PostObject>();
mrobraven commented 3 years ago

@mrobraven if comments is a subcollection of posts, to get it you must do something like:

CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").Collection("comments").Document("randomID").GetAsync();

to map to your object:

var document= await CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync();
var post = document.ToObject<PostObject>();

I'm not trying to return the contents of the subcollections, I'm trying to reference the document that the subcollections are in

mrobraven commented 3 years ago

This line: postRef = CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync(); isn't working. It freezes and then times out without giving a response or exception, crashing the application.

angelru commented 3 years ago

try: postRef = await CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync();

mrobraven commented 3 years ago

That is the line I have used that isn't working. When the app runs, I can see the task timing out in the console at which point the app crashes.

angelru commented 3 years ago

Are you sure you have toawait?await CrossCloudFirestore

mrobraven commented 3 years ago

yes. Is there a better way of catching a specific exception without the application crashing? at the minute I have this:

            try
            {
                var response = await CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync();
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    throw e;
                }
            }

but the application just crashes without returning a response.

angelru commented 3 years ago

ummm, I think the problem comes from elsewhere. can you put more code from that call?

mrobraven commented 3 years ago

This is the method snippet:

public async Task<Dictionary<string, object>> GetPost(string postID)
        {
            Console.WriteLine("RUNNING. RECEIVED postID: " + postID); // this line appears in the console

            var snapshot = await CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync();

            Console.WriteLine("RESPONSE RECEIVED FROM " + snapshot.Reference.Path); // this line and below never runs

            var post = snapshot.ToObject<Dictionary<string, object>>();

            Console.WriteLine("POST DICT MADE: " + post);

            return post;

        }

Indicated by the comments, you can see that the line beginning var snapshot = is where the app crashes. The weird thing is, await CrossCloudFirestore.Current.Instance.Collection("posts").GetAsync(); works and returns a list of documents. I've used it elsewhere in the project whereas await CrossCloudFirestore.Current.Instance.Collection("posts").Document("dTBc9SOucPh01LZiit24").GetAsync(); doesn't return a single document.

mrobraven commented 3 years ago

Here is a photo of the database Screenshot 2021-04-19 at 19 05 42

angelru commented 3 years ago

where do you run GetPost? I don't see anything weird ... I make a lot of calls like that and it never gets blocked

mrobraven commented 3 years ago

Fixed it. I was running it from a Task.Run() situation which the GetAsync didn't like. I switched the method the call is within to async and used the await operator to run the GetPost() method and now it works!