Android parse.com setObjectId - android

How set objectId with Parse.com with primary key.Can you help me ?
When i create new row, i want setObjectId of row.
final ParseObject parseObject = new ParseObject(ChapsModel.PARSE_OBJECT);
parseObject.put(ChapsModel.PARSE_FIELD_NAME_CHAPS,
chapsModel.get(i)
.getNamChap());
parseObject.put(ChapsModel.PARSE_FIELD_LINK_CHAP,
chapsModel.get(i)
.getLinkChap());
parseObject.put(ChapsModel.PARSE_FILED_TEAM_TRANSLATE,
chapsModel.get(i)
.getTeamTranslate());
parseObject.put(ChapsModel.PARSE_FIELD_OBJECT_MANGA,
ParseObject.createWithoutData(MangaModel.PARSE_OBJECT,
chapsModel.get(i)
.getObjectManga()));
parseObject.saveInBackground(new SaveCallback() {
#Override
public void done(final ParseException e) {
if (e == null) {
Log.e(">>>>>",
"done" + ojectId);
// parseObject.setObjectId(ojectId);
} else {
Log.e(">>>>>",
"else" + e.getMessage());
}
}
});
Log
java.lang.RuntimeException: objectIds cannot be changed in offline mode.
Log:
Sorry my english. thank

It's not possible to set objectId with Parse.com with primary key.
Although parse.com doesn't allow us to set the objectId of a row, you can create a new column named anything you want and you can set that new column to any objectId you want. For example, you can create a new column named myObjectId and set it to a string.
From the parse.com website at https://www.parse.com/docs/js/guide#cloud_code
The Data Browser
The Data Browser is the web UI where you can update and create objects in each of your apps. Here, you can see the raw JSON values that are saved that represents each object in your class.
When using the interface, keep in mind the following:
The objectId, createdAt, updatedAt fields cannot be edited (these are set automatically).

Related

How to make sure that Data are saved in Parse.com and ready to be retrieved Android

I'm using Parse.com for developing a small application.
In a couple of actives i'm inserting values to a Class, and Immediately after that i'm Retrieving the values inserted from the previous activity, it's showing Error, it seems that it takes some seconds to save before being able to retrieve.
so i'm asking how can I know that the saved has been completed with Parse. is there any callback function which can let me know, so I would be able to retrieve that information
here is the code where i'm saving values
ParseUser user = ParseUser.getCurrentUser();
ParseQuery<ParseObject> query = ParseQuery.getQuery("test");
query.whereEqualTo("user", user.getUsername());
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
} else {
file = new ParseFile("profil.png", image);
file.saveInBackground();
object.put("name", nom.getText().toString());
object.put("Profil",file);
object.saveInBackground();
}
I have found an answer to my question
https://parse.com/docs/android/api/com/parse/SaveCallback.html
thank to Bjorn Kaiser

Android | Parse.com | Query User Collection with another Collection

i have basic question. Iam using parse android SDK to retrieve some data from backend. I have 2 collections : "User" and "Photo". What i want to do is that i want to query "Photo" collection which contains some data about image and join that against "User" collection and get some info about owner of picture. Please see code below:
ParseQuery<ParseUser> queryInner = ParseUser.getQuery();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Photo");
query.whereMatchesKeyInQuery("owner", "username", queryInner);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> resultSet, ParseException exception) {
if (exception == null) {
parsqQueryResult = new ArrayList<ObjectUserPhoto>();
ObjectUserPhoto row = new ObjectUserPhoto();
Log.e("size",String.valueOf(resultSet.size()));
for (int a = 0; a < resultSet.size(); a++) {
row = new ObjectUserPhoto();
//this works
row.setDescription(resultSet.get(a).getString(Constants.DB_COL_USER_PHOTOS_DESCRIPTION));
//this is not working
Log.e("username",String.valueOf(resultSet.get(a).getString("col_from_user_table")));
parsqQueryResult.add(row);
}
}
}
});
So basically iam able to acces columns from "Photo" collection, but iam not able to acces any column from "User" collection. How can i acces data from "User" collection ??
Thanks
Martin
In your comment you mentioned that you are not using pointers. I assume that you are just storing the id of the ParseUser objects.
Given a ParseObject's ID, you can retrieve an object by issuing a ParseQuery and passing the ID. Refer to this document.
However, I do not see any reason why you would want to store just the ID and not the Pointer to the object. Imagine if you want to display the information on a list then that means for every item you will need to perform a ParseQuery.
If you store a Pointer then you can leverage on the "include" method (see it here at the bottom part of the "Relational Queries"). This allows you to include the actual object the Pointer is pointing to. So in your case, the results of the Photo query will include the User objects too.
And lastly, I suggest that you read through at least the whole Objects and Queries sections of the documentation as you would learn the basics on how to use the SDK.

Parse.com pointer data for updating tables using Android

I am making an application for saving user information. I have successfully updated my manual table "UserInfo". Now I want to get the data from that table by using Current User Phenomena. My code snippet is given below:
final ParseUser user = ParseUser.getCurrentUser();
user.put("firstName", fName.getText().toString());
user.put("lastName", lName.getText().toString());
user.put("email", mailText.getText().toString());
// Here I am updating data in UserInfo Table, I want to get
// values from this table now, I mean I want to retrieve data here.
final ParseObject update = new ParseObject("UserInfo");
update.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException arg0) {
update.put("doctor", doInfo.getText().toString());
update.put("medi", medtion.getText().toString());
update.put("icalNote", medNotes.getText().toString());
update.put("meCondition", medCond.getText().toString());
update.put("agies", agiesReact.getText().toString());
update.saveInBackground();
}
});
in your UserInfo class you add new field 'ptrUser' that is type pointer and that has value = getCurrentUser().
When you have saved the UserInfo with the ptr field, you can do a flat query retreiving both of the classes
in REST API idiom :
GET ./classes/UserInfo/null?include=ptrUser
SDK idiom:
query.include($ptrUser)

Access New ParseObject (Parse.com) Across Activities (Android)

I'm looking for a way to access a newly created local ParseObject which hasn't yet synced to the Parse cloud server. Since there is no objectId value there's no way to query for the objectId through the local datastore and it appears the localId (which looks like it creates a unique identifier locally) is locked down (otherwise this would be a non-issue as I could use my Content Provider to take care of the details). Since the ParseObject class isn't Serializable of Parcelable I can't pass it through an Intent. To note the complexity of my task I have I have 3 levels of ParseObjects (ParseObject > Array[ParseObjects] > Array[ParseObjects]). Essentially I'm looking to see if Parse has full offline capabilities.
TL:DR
Basically I want to be able to access a single ParseObject in a different Activity as soon as it's created. Does this problem have a practical application with Parse and ParseObjects or am I going to have to implement some serious work arounds?
I believe ParseObjects are serializable, so put them into a Bundle and then put that Bundle into an Intent
in the current activity
Intent mIntent = new Intent(currentActivityReference, DestinationActivity.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("object", mParseObject);
mIntent.putExtras(mBundle);
startActivity(mIntent);
in the destination activity
retrieve the intent with getIntent().getExtras(), which is a Bundle object, so there is a getter for the serializable .getSerializable("object") but you will have to cast it to (ParseObject)
So I was able to keep everything within the confines of the structures I already have in place to take care of this problem (a sync adapter and the Parse API). Basically all I had to do was leverage Parse's existing "setObjectId" function.
NOTE: This only works with an existing Content Provider / SQLiteDatabase
I created a temporary unique ID for the new ParseObject to be stored locally. This unique value is based off of the max index number in the Content Provider I'm storing my objects (for my Sync Adapter).
//query to get the max ID from the Content Provider (used with the sync adapter)
Cursor cursor = context.getContentResolver().query(
WorkoutContract.Entry.CONTENT_URI,
new String[]{"MAX(" + WorkoutContract.Entry._ID + ")"},
null, null, null
);
long idx = 1; //default max index if there are no records
if (cursor.moveToFirst())
idx = cursor.getInt(0) + 1;
final long maxIndex = idx;
cursor.close();
//this is the temporary ID used for storing, a String constant prepended to the max index
String localID = WorkoutContract.LOCAL_WORKOUT_ID + maxIndex;
I then used the pin() method to store this ParseObject locally and then made an insert into the Content Provider to not only keep the ID in the table to iterate the max index in the table.
//need to insert a dummy value into the Content Provider so the max _ID iterates
ContentValues workoutValues = new ContentValues();
//the COLUMN_WORKOUT_ID constant refers to the column which holds the ParseObjects ID
workoutValues.put(WorkoutContract.Entry.COLUMN_WORKOUT_ID, localID);
context.getContentResolver().insert(
WorkoutContract.Entry.CONTENT_URI,
workoutValues);
Then I created another dummy ParseObject with all the same attributes as the one with the local ID (without the local ID). This ParseObject was then saved to the server via the saveEventually() function. (Note: This will create 2 local copies or your ParseObject. To leave the blank copy out of queries simply leave out ParseObjects with null object IDs).
query.whereNotEqualTo("objectId", null);
In the saveEventually() function there needs to be a callback which replaces the old (local) ParseObject as well as the localID value in the Content provider. In the SaveCallback object replace the server returned ParseObject's attributes with the local ones (to account for any changes made during the server query). Below is the full code for the SaveCallback where the tempObject is the one sent to the Parse server:
tempObject.saveEventually(new SaveCallback() {
//changes the local ParseObject's ID to the newly generated one
#Override
public void done(ParseException e) {
if (e == null) {
try {
//replaces the old ParseObject
tempObject.put(Workout.PARSE_FIELD_NAME, newWorkout.get(Workout.PARSE_FIELD_NAME));
tempObject.put(Workout.PARSE_FIELD_OWNER, ParseUser.getCurrentUser());
tempObject.put(Workout.PARSE_FIELD_DESCRIPTION, newWorkout.get(Workout.PARSE_FIELD_DESCRIPTION));
tempObject.pin();
newWorkout.unpinInBackground(new DeleteCallback() {
#Override
public void done(ParseException e) {
Log.i(TAG, "Object unpinned");
}
});
} catch (ParseException e1) {
e1.printStackTrace();
}
//update to content provider with the new ID
ContentValues mUpdateValues = new ContentValues();
String mSelectionClause = WorkoutContract.Entry._ID + "= ?";
String[] mSelectionArgs = {Long.toString(maxIndex)};
mUpdateValues.put(WorkoutContract.Entry.COLUMN_WORKOUT_ID, tempObject.getObjectId());
mUpdateValues.put(WorkoutContract.Entry.COLUMN_UPDATED, tempObject.getUpdatedAt().getTime());
context.getContentResolver().update(
WorkoutContract.Entry.CONTENT_URI,
mUpdateValues,
mSelectionClause,
mSelectionArgs
);
}
}
});
To get the local ParseObject in another Activity just pass the local objectId in an Intent and load it. However, the index of the ParseObject on the Content Provider needs to be passed as well (or it can be retrieved from the unique local ID) so if the ParseObject is ever retrieved again you can check the Content Provider for the updated Object ID and query the correct ParseObject.
This could use a bit of refinement but for now it works.

How to get objectId on Parse.com android

It is said that we can retrieve our data if we are having objectId for that particular row, but it is auto generated and we cant insert it while setting data , so how to get data if i am not having object id , or any other means so that i can set objectId on my means.
Code is here as in comment:
ParseObject gameScore = new ParseObject("My Parse File");
String objectId = gameScore.getObjectId();
ObjectId doesnt't exist until a save operation is completed.
ParseObject gameScore = new ParseObject("My Parse File");
To retrieve the object id you need to save the object and register for the save callback.
gameScore.saveInBackground(new SaveCallback <ParseObject>() {
public void done(ParseException e) {
if (e == null) {
// Success!
String objectId = gameScore.getObjectId();
} else {
// Failure!
}
}
});
ObjectId can be retrieved from the original ParseObject(gameScore) once the done save callback is fired.
You can use this for getting current user object id
ParseUser pUser= ParseUser.getCurrentUser();
String objId= pUser.getObjectId();
not sure if this will apply to android , but I was trying to retreive the objectid, but for an entry that is already created. I did something like this and it worked.
ParseObject gameScore = new ParseObject("My Parse File");
var obId = gameScore.id;
Got it from the Javascript docs on Parse.com
The three special values are provided as properties:
var objectId = gameScore.id;
var updatedAt = gameScore.updatedAt;
var createdAt = gameScore.createdAt;
You can't unfortunately use the ObjectId until the object's been saved. I'm having this same problem now. The only solution I can think of, and posted a similar question relating to it here
My solution would be to make a tempId on the object and refer to that locally until it has an actual ObjectId, perhaps using a saveInBackground or saveEventually() callback to set other objects relating to it, to it's newly created ObjectId instead of it's old temp one.. when it's made available
It takes times for your values to be stored in table. Use this to get ObjectId
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// Saved successfully.
Log.d("main", "User update saved!");
Log.d("main", "ObjectId "+gameScore.getObjectId());
} else {
// The save failed.
Log.d("main", "User update error: " + e);
}
}
});
In case you're handling the thread yourself:
First save:
gameScore.save();
Then you'll be able to access the id:
String parseId = gameScore.getObjectId();

Categories

Resources