Question

Currently, I'm sending some data to Parse.com. All works well, however, I would like to add a row if it's a new user or update the current table if it's an old user.

So what I need to do is check if the current Facebook ID (the key I'm using) shows up anywhere in the fbid column, then update it if case may be.

How can I check if the key exists in the column?

Also, I'm using C#/Unity.

static void sendToParse()
    {
        ParseObject currentUser = new ParseObject("Game");
        currentUser["name"] = fbname;
        currentUser["email"] = fbemail;
        currentUser["fbid"] = FB.UserId;
        Task saveTask = currentUser.SaveAsync();
        Debug.LogError("Sent to Parse");
    }
Was it helpful?

Solution

Okay, I figured it out.

First, I check which if there is any Facebook ID in the table that matches the current ID, then get the number of matches.

public static void getObjectID()
    {
        var query = ParseObject.GetQuery("IdealStunts")
               .WhereEqualTo("fbid", FB.UserId);
        query.FirstAsync().ContinueWith(t =>
        {
            ParseObject obj = t.Result;
            objectID = obj.ObjectId;
            Debug.LogError(objectID);
        });
    }

If there is any key matching the current Facebook ID, don't do anything. If there aren't, just add a new user.

public static void sendToParse()
    {

        if (count != 0)
        {
            Debug.LogError("Already exists");
        }
        else
        {
            ParseObject currentUser = new ParseObject("IdealStunts");
            currentUser["name"] = fbname;
            currentUser["email"] = fbemail;
            currentUser["fbid"] = FB.UserId;
            Task saveTask = currentUser.SaveAsync();
            Debug.LogError("New User");
        }
    }

You will have to do a StartCoroutine for sendToParse, so getObjectID has time to look through the table.

It may be a crappy implementation, but it works.

OTHER TIPS

What you need to do is create a query for the fbid. If the query returns an object, you update it. If not, you create a new.

I'm not proficient with C#, but here is an example in Objective-C:

PFQuery *query = [PFQuery queryWithClassName:@"Yourclass]; // Name of your class in Parse
query.cachePolicy = kPFCachePolicyNetworkOnly;
[query whereKey:@"fbid" equalTo:theFBid];  // Variable containing the fb id
NSArray *users = [query findObjects];
self.currentFacebookUser = [users lastObject];  // Array should contain only 1 object

if (self.currentFacebookUser) {  // Might have to test for NULL, but probably not
    // Update the object and save it
} else {
    // Create a new object
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top