/** * Deletes this object on the server. This does not delete or destroy the object locally. * * @throws ParseException Throws an error if the object does not exist or if the internet fails. */ public final void delete() throws ParseException { ParseTaskUtils.wait(deleteInBackground()); }
/** * Access a {@link ParseFile} value. This function will not perform a network request. Unless the * {@link ParseFile} has been downloaded (e.g. by calling {@link ParseFile#getData()}), * {@link ParseFile#isDataAvailable()} will return {@code false}. * * @param key The key to access the value for. * @return {@code null} if there is no such key or if it is not a {@link ParseFile}. */ @Nullable public ParseFile getParseFile(@NonNull String key) { Object value = get(key); if (!(value instanceof ParseFile)) { return null; } return (ParseFile) value; }
public boolean hasSameId(ParseObject other) { synchronized (mutex) { return this.getClassName() != null && this.getObjectId() != null && this.getClassName().equals(other.getClassName()) && this.getObjectId().equals(other.getObjectId()); } }
boolean isDirty(boolean considerChildren) { synchronized (mutex) { return (isDeleted || getObjectId() == null || hasChanges() || (considerChildren && hasDirtyChildren())); } }
private String getObjectOrLocalId(ParseObject object) { return object.getObjectId() != null ? object.getObjectId() : object.getOrCreateLocalId(); } }
@Test public void testParcelable() { ParseObject object = ParseObject.createWithoutData("Test", "objectId"); object.isDeleted = true; object.put("long", 200L); object.put("double", 30D); object.put("int", 50); object.put("string", "test"); object.put("date", new Date(200)); object.put("null", JSONObject.NULL); object.put("collection", Arrays.asList("test1", "test2")); ParseObject other = ParseObject.createWithoutData("Test", "otherId"); object.put("pointer", other); object.put("map", map); object.put("bytes", bytes); object.setACL(acl); ParseObject related = ParseObject.createWithoutData("RelatedClass", "relatedId"); ParseRelation<ParseObject> rel = new ParseRelation<>(object, "relation"); rel.add(related); object.put("relation", rel); object.put("file", file); object.put("point", point);
ParseObject object = new ParseObject("TestObject"); object.setObjectId("id"); object.put("key", "value"); object.put("number", 5); tasks.add(object.saveInBackground()); object.put("key", "newValue"); object.increment("number", 6); tasks.add(object.saveInBackground()); object.increment("number", -1); tasks.add(object.saveInBackground()); assertTrue(object.hasOutstandingOperations()); Parcel parcel = Parcel.obtain(); object.writeToParcel(parcel, 0); parcel.setDataPosition(0); ParseObject other = ParseObject.CREATOR.createFromParcel(parcel); assertTrue(other.isDirty("key")); assertTrue(other.isDirty("number")); assertEquals(other.getString("key"), "newValue"); assertEquals(other.getNumber("number"), 10); assertFalse(other.hasOutstandingOperations());
ParseObject object = new ParseObject("TestObject"); object.revert("foo"); object.put("foo", "bar"); object.put("name", "grantland"); object.revert("foo"); assertNull(object.get("foo")); assertEquals("grantland", object.get("name")); when(state.keySet()).thenReturn(Collections.singleton("foo")); when(state.get("foo")).thenReturn("bar"); object = ParseObject.from(state); object.revert("foo"); assertFalse(object.isDirty()); assertEquals("bar", object.get("foo")); object.put("foo", "baz"); object.put("name", "grantland"); object.revert("foo"); assertEquals("bar", object.get("foo")); assertEquals("grantland", object.get("name")); object.put("foo", "baz"); object.put("name", "nlutsenko"); tasks.add(object.saveInBackground()); object.revert("foo"); assertEquals("baz", object.get("foo")); assertEquals("nlutsenko", object.get("name"));
assertEquals(className, object.getClassName()); long dateLong = ParseDateFormat.getInstance().parse(objectJSON.getString("createdAt")).getTime(); assertEquals(dateLong, object.getState().createdAt()); dateLong = ParseDateFormat.getInstance().parse(objectJSON.getString("updatedAt")).getTime(); assertEquals(dateLong, object.getState().updatedAt()); assertEquals(objectJSON.getString("objectId"), object.getObjectId()); assertEquals(objectJSON.getString("sessionToken"), object.get("sessionToken")); assertEquals(objectJSON.getString("key"), object.getString("key")); assertEquals(className, objectAgain.getClassName()); JSONObject objectAgainJSON = objectsJSON.getJSONObject(1); dateLong = ParseDateFormat.getInstance().parse(objectAgainJSON.getString("createdAt")).getTime(); assertEquals(dateLong, objectAgain.getState().createdAt()); dateLong = ParseDateFormat.getInstance().parse(objectAgainJSON.getString("updatedAt")).getTime(); assertEquals(dateLong, objectAgain.getState().updatedAt()); assertEquals(objectAgainJSON.getString("objectId"), objectAgain.getObjectId()); assertEquals(objectAgainJSON.getString("sessionToken"), objectAgain.get("sessionToken")); assertEquals(objectAgainJSON.getString("keyAgain"), objectAgain.getString("keyAgain"));
if (!object.isDataAvailable()) { throw new ParseException(ParseException.INVALID_NESTED_KEY, String.format("Bad key: %s", key)); return object.getObjectId(); case "createdAt": case "_created_at": return object.getCreatedAt(); case "updatedAt": case "_updated_at": return object.getUpdatedAt(); default: return object.get(key);
/** * Registers the Parse-provided {@code ParseObject} subclasses. Do this here in a real method rather than * as part of a static initializer because doing this in a static initializer can lead to * deadlocks */ static void registerParseSubclasses() { registerSubclass(ParseUser.class); registerSubclass(ParseRole.class); registerSubclass(ParseInstallation.class); registerSubclass(ParseSession.class); registerSubclass(ParsePin.class); registerSubclass(EventuallyPin.class); }
@Test public void testSortDescending() throws ParseException { ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject") .addDescendingOrder("name") .build(); List<ParseObject> objects = new ArrayList<>(); ParseObject object; object = new ParseObject("TestObject"); object.put("name", "grantland"); objects.add(object); object = new ParseObject("TestObject"); object.put("name", "nikita"); objects.add(object); object = new ParseObject("TestObject"); object.put("name", "listiarso"); objects.add(object); OfflineQueryLogic.sort(objects, query); assertEquals("nikita", objects.get(0).getString("name")); assertEquals("listiarso", objects.get(1).getString("name")); assertEquals("grantland", objects.get(2).getString("name")); }
@Test public void testParcelWhileDeleting() throws Exception { mockCurrentUserController(); TaskCompletionSource<Void> tcs = mockObjectControllerForDelete(); ParseObject object = new ParseObject("TestObject"); object.setObjectId("id"); Task<Void> deleteTask = object.deleteInBackground(); // ensure Log.w is called.. assertTrue(object.isDeleting); Parcel parcel = Parcel.obtain(); object.writeToParcel(parcel, 0); parcel.setDataPosition(0); ParseObject other = ParseObject.CREATOR.createFromParcel(parcel); // By design, when LDS is off, we assume that old operations failed even if // they are still running on the old instance. assertFalse(other.isDeleting); assertTrue(object.isDeleting); tcs.setResult(null); deleteTask.waitForCompletion(); assertFalse(object.isDeleting); assertTrue(object.isDeleted); }
ParseObject request = new ParseObject("Requests"); request.put("requesterUsername", "1");//ParseUser.getCurrentUser().getUsername()); ParseACL parseACL = new ParseACL(); parseACL.setPublicWriteAccess(true); parseACL.setPublicReadAccess(true); request.setACL(parseACL); request.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) {
@Test public void testGetParseGeoPointWithWrongValue() { ParseObject object = new ParseObject("Test"); object.put("key", 1); assertNull(object.getParseGeoPoint("key")); }
@Test public void testRegisteringSubclassesUsesMostDescendantSubclass() { try { // When we register a ParseUser subclass, we have to clear the cached currentParseUser, so // we need to register a mock ParseUserController here, otherwise Parse.getCacheDir() will // throw an exception in unit test environment. ParseCurrentUserController controller = mock(ParseCurrentUserController.class); ParseCorePlugins.getInstance().registerCurrentUserController(controller); assertEquals(ParseUser.class, ParseObject.create("_User").getClass()); ParseObject.registerSubclass(MyUser.class); assertEquals(MyUser.class, ParseObject.create("_User").getClass()); ParseObject.registerSubclass(ParseUser.class); assertEquals(MyUser.class, ParseObject.create("_User").getClass()); // This is expected to fail as MyUser2 and MyUser are not directly related. try { ParseObject.registerSubclass(MyUser2.class); fail(); } catch (IllegalArgumentException ex) { /* expected */ } assertEquals(MyUser.class, ParseObject.create("_User").getClass()); } finally { ParseObject.unregisterSubclass(ParseUser.class); ParseCorePlugins.getInstance().reset(); } }
/** * Stores the objects and every object they point to in the local datastore, recursively. If * those other objects have not been fetched from Parse, they will not be stored. However, if they * have changed data, all of the changes will be retained. To get the objects back later, you can * use {@link ParseQuery#fromLocalDatastore()}, or you can create an unfetched pointer with * {@link #createWithoutData(Class, String)} and then call {@link #fetchFromLocalDatastore()} on it. * * @param objects the objects to be pinned * @return A {@link bolts.Task} that is resolved when pinning all completes. * @see #unpinAllInBackground(java.util.List) * @see #DEFAULT_PIN */ public static <T extends ParseObject> Task<Void> pinAllInBackground(List<T> objects) { return pinAllInBackground(DEFAULT_PIN, objects); }
@Override public void done(List<ParseObject> objects, ParseException e) { if ( e == null){ if(objects.size() > 0){ listViewContent.clear(); usernames.clear(); latitudes.clear(); longitudes.clear(); for (ParseObject object : objects){ Double distanceInMiles = userLocation.distanceInMilesTo((ParseGeoPoint) object.get("requesterLocation")); Double distaneOneDP = (double) Math.round(distanceInMiles *10) / 10; listViewContent.add(String.valueOf(distaneOneDP)+ " miles"); usernames.add(object.getString("requesterUsername")); latitudes.add(object.getParseGeoPoint("requesterLocation").getLatitude()); longitudes.add(object.getParseGeoPoint("requesterLocation").getLongitude()); } arrayAdapter.notifyDataSetChanged(); } } } });
@Test public void testGetJsonObjectWithWrongValue() { ParseObject object = new ParseObject("Test"); object.put("key", 1); assertNull(object.getJSONObject("key")); }
@Test public void testGetDouble() { ParseObject object = new ParseObject("Test"); object.put("key", 1.1); assertEquals(1.1, object.getDouble("key"), 0.00001); }