简体   繁体   中英

C# desktop app and Parse.com

I have a desktop application written in C# (not a windows 8 app) that is talking to the Parse database and there is no problem with creating a new account, logging in and transferring information to the user object.

However, when it comes to getting that information out,that is where the problem lies.

This code works (edited) and returns the Credit value:

ParseQuery<ParseObject> query = ParseObject.GetQuery("User");
ParseObject CreditAmount = query.GetAsync("XcaSDFrGGhX");

int Credit = CreditAmount.Get<int>("Credit");

This code does not work:

ParseQuery<ParseObject> query = ParseObject.GetQuery("User");
ParseObject CreditAmount = query.GetAsync(ParseUser.CurrentUser.ObjectID);

int Credit = CreditAmount.Get<int>("Credit");

An exception is thrown: Object with the given objectId found.

Parse.ParseException.ErrorCode.ObjectNotFound

suggesting the user objectID has been found but there is something wrong, Why?

Also, it would be more advantageous if the Parse.User could be declared as a global variable. How is this done as in the example above it's declared as a local Var within the sub?

I don't know C#, but this is my take:

On the working code, you get a specific CreditAmount object (which is actually a User object), which has an objectId of "XcaSDFrGGhX".

On the second, you try to query for a user object by using the objecId of the current user. So, what I think you are trying to do is to get the "Credit" field from the current user? If that is the case, you can use CurrentUser directly. ParseUser.CurrentUser is always available to you, and should be able to get the property with something like like this:

int Credit = ParseUser.CurrentUser.Get<int>("Credit");

First off, from the code you have posted there's no sign that you are waiting for the Async task to finish. In C# you would use the await keyword so that the following lines of code are not executed until the async operation finishes.

// NOTE: the 'await' keyword here after the '='
ParseObject user = await query.GetAsync(ParseUser.CurrentUser.ObjectID);
// this code will be delayed until the async-get is done
int credit = user.Get<int>("Credit");

Secondly, if you want to use the current user , the ParseUser.CurrentUser is already loaded. You can tell it to update if needed using FetchAsync() but generally you just use it:

// no query needed
int credit = ParseUser.CurrentUser.Get<int>("Credit");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM