Skip to content

Commit 0774ee6

Browse files
hyperrecursivephillwiggins
authored andcommitted
Object is not dirty after fromJson. forceUpdate is not ignored. async/await fixes from master. (#254)
* Added some missing async/await (#248) * Updated dependencies. * ParseUser._handleResponse was calling saveInStorage() async without an await, which caused 'invalid session token' error if user ParseUser.currentUser() immediately after ParseUser.signUp() or ParseUser.login(). Added missing awaits in other places. * ParseUser was not updated in storage after a successful save() operation. Reason: no safeInStorage() after save() was called. Fix: calling saveInStorage() after save(). * Revert "Updated dependencies." This reverts commit dbbc5e8. * forceUpdate was ignored when setting ParseBase properties * Setting objectData properties directly in parseJson function to prevent making decoded object properties dirty * Made forceUpdate false by default in ParseBase set method * Revert incorrect merge
1 parent e49b521 commit 0774ee6

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

lib/src/objects/parse_base.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,23 @@ abstract class ParseBase {
121121
if (key == parseClassName || key == '__type') {
122122
// NO OP
123123
} else if (key == keyVarObjectId) {
124-
objectId = value;
124+
_getObjectData()[keyVarObjectId] = value;
125125
} else if (key == keyVarCreatedAt) {
126126
if (keyVarCreatedAt is String) {
127-
set<DateTime>(keyVarCreatedAt, _parseDateFormat.parse(value));
127+
_getObjectData()[keyVarCreatedAt] = _parseDateFormat.parse(value);
128128
} else {
129-
set<DateTime>(keyVarCreatedAt, value);
129+
_getObjectData()[keyVarCreatedAt] = value;
130130
}
131131
} else if (key == keyVarUpdatedAt) {
132132
if (keyVarUpdatedAt is String) {
133-
set<DateTime>(keyVarUpdatedAt, _parseDateFormat.parse(value));
133+
_getObjectData()[keyVarUpdatedAt] = _parseDateFormat.parse(value);
134134
} else {
135-
set<DateTime>(keyVarUpdatedAt, value);
135+
_getObjectData()[keyVarUpdatedAt] = _parseDateFormat.parse(value);
136136
}
137137
} else if (key == keyVarAcl) {
138-
this[keyVarAcl] = ParseACL().fromJson(value);
138+
_getObjectData()[keyVarAcl] = ParseACL().fromJson(value);
139139
} else {
140-
this[key] = parseDecode(value);
140+
_getObjectData()[key] = parseDecode(value);
141141
}
142142
});
143143

@@ -177,18 +177,18 @@ abstract class ParseBase {
177177
/// Saves in storage
178178
Future<void> saveInStorage(String key) async {
179179
final String objectJson = json.encode(toJson(full: true));
180-
ParseCoreData().getStore()..setString(key, objectJson);
180+
await ParseCoreData().getStore().setString(key, objectJson);
181181
}
182182

183183
/// Sets type [T] from objectData
184184
///
185185
/// To set an int, call setType<int> and an int will be saved
186186
/// [bool] forceUpdate is always true, if unsure as to whether an item is
187187
/// needed or not, set to false
188-
void set<T>(String key, T value, {bool forceUpdate = true}) {
188+
void set<T>(String key, T value, {bool forceUpdate = false}) {
189189
if (value != null) {
190190
if (_getObjectData().containsKey(key)) {
191-
if (_getObjectData()[key] == value) {
191+
if (_getObjectData()[key] == value && !forceUpdate) {
192192
return;
193193
}
194194
_getObjectData()[key] =
@@ -240,7 +240,7 @@ abstract class ParseBase {
240240
await unpin();
241241
final Map<String, dynamic> objectMap = parseEncode(this, full: true);
242242
final String json = jsonEncode(objectMap);
243-
ParseCoreData().getStore()..setString(objectId, json);
243+
await ParseCoreData().getStore().setString(objectId, json);
244244
return true;
245245
} else {
246246
return false;
@@ -252,7 +252,7 @@ abstract class ParseBase {
252252
/// Replicates Android SDK pin process and saves object to storage
253253
Future<bool> unpin({String key}) async {
254254
if (objectId != null) {
255-
ParseCoreData().getStore()..remove(key ?? objectId);
255+
await ParseCoreData().getStore().remove(key ?? objectId);
256256
return true;
257257
}
258258

lib/src/objects/parse_installation.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,17 @@ class ParseInstallation extends ParseObject {
204204
}
205205

206206
///Subscribes the device to a channel of push notifications.
207-
void subscribeToChannel(String value) {
207+
Future<void> subscribeToChannel(String value) async {
208208
final List<dynamic> channel = <String>[value];
209209
setAddAllUnique('channels', channel);
210-
save();
210+
await save();
211211
}
212212

213213
///Unsubscribes the device to a channel of push notifications.
214-
void unsubscribeFromChannel(String value) {
214+
Future<void> unsubscribeFromChannel(String value) async {
215215
final List<dynamic> channel = <String>[value];
216216
setRemove('channels', channel);
217-
save();
217+
await save();
218218
}
219219

220220
///Returns an <List<String>> containing all the channel names this device is subscribed to.

lib/src/objects/parse_user.dart

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
105105
try {
106106
final Uri url = getSanitisedUri(_client, '$keyEndPointUserName');
107107
final Response response = await _client.get(url, headers: headers);
108-
return _handleResponse(_getEmptyUser(), response, ParseApiRQ.currentUser,
108+
return await _handleResponse(_getEmptyUser(), response, ParseApiRQ.currentUser,
109109
_debug, _getEmptyUser().parseClassName);
110110
} on Exception catch (e) {
111111
return handleException(
@@ -146,7 +146,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
146146
},
147147
body: body);
148148

149-
return _handleResponse(
149+
return await _handleResponse(
150150
this, response, ParseApiRQ.signUp, _debug, parseClassName);
151151
} on Exception catch (e) {
152152
return handleException(e, ParseApiRQ.signUp, _debug, parseClassName);
@@ -172,7 +172,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
172172
keyHeaderRevocableSession: '1',
173173
});
174174

175-
return _handleResponse(
175+
return await _handleResponse(
176176
this, response, ParseApiRQ.login, _debug, parseClassName);
177177
} on Exception catch (e) {
178178
return handleException(e, ParseApiRQ.login, _debug, parseClassName);
@@ -195,7 +195,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
195195
}
196196
}));
197197

198-
return _handleResponse(
198+
return await _handleResponse(
199199
this, response, ParseApiRQ.loginAnonymous, _debug, parseClassName);
200200
} on Exception catch (e) {
201201
return handleException(
@@ -222,7 +222,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
222222
'authData': <String, dynamic>{provider: authData}
223223
}));
224224

225-
return _handleResponse(
225+
return await _handleResponse(
226226
this, response, ParseApiRQ.loginWith, _debug, parseClassName);
227227
} on Exception catch (e) {
228228
return handleException(e, ParseApiRQ.loginWith, _debug, parseClassName);
@@ -248,7 +248,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
248248
final Response response = await _client.post(url,
249249
headers: <String, String>{keyHeaderSessionToken: sessionId});
250250

251-
return _handleResponse(
251+
return await _handleResponse(
252252
this, response, ParseApiRQ.logout, _debug, parseClassName);
253253
} on Exception catch (e) {
254254
return handleException(e, ParseApiRQ.logout, _debug, parseClassName);
@@ -261,7 +261,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
261261
final Response response = await _client.post(
262262
'${_client.data.serverUrl}$keyEndPointVerificationEmail',
263263
body: json.encode(<String, dynamic>{keyVarEmail: emailAddress}));
264-
return _handleResponse(this, response,
264+
return await _handleResponse(this, response,
265265
ParseApiRQ.verificationEmailRequest, _debug, parseClassName);
266266
} on Exception catch (e) {
267267
return handleException(
@@ -275,8 +275,9 @@ class ParseUser extends ParseObject implements ParseCloneable {
275275
final Response response = await _client.post(
276276
'${_client.data.serverUrl}$keyEndPointRequestPasswordReset',
277277
body: json.encode(<String, dynamic>{keyVarEmail: emailAddress}));
278-
return _handleResponse(this, response, ParseApiRQ.requestPasswordReset,
279-
_debug, parseClassName);
278+
return await _handleResponse(
279+
this, response, ParseApiRQ.requestPasswordReset, _debug,
280+
parseClassName);
280281
} on Exception catch (e) {
281282
return handleException(
282283
e, ParseApiRQ.requestPasswordReset, _debug, parseClassName);
@@ -290,19 +291,27 @@ class ParseUser extends ParseObject implements ParseCloneable {
290291
@override
291292
Future<ParseResponse> save() async {
292293
if (objectId == null) {
293-
return signUp();
294+
return await signUp();
294295
} else {
295-
return super.save();
296+
final ParseResponse response = await super.save();
297+
if (response.success) {
298+
await _onResponseSuccess();
299+
}
300+
return response;
296301
}
297302
}
298303

304+
Future<void> _onResponseSuccess() async {
305+
await saveInStorage(keyParseStoreUser);
306+
}
307+
299308
/// Removes a user from Parse Server locally and online
300309
Future<ParseResponse> destroy() async {
301310
if (objectId != null) {
302311
try {
303312
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
304313
final Response response = await _client.delete(url);
305-
return _handleResponse(
314+
return await _handleResponse(
306315
this, response, ParseApiRQ.destroy, _debug, parseClassName);
307316
} on Exception catch (e) {
308317
return handleException(e, ParseApiRQ.destroy, _debug, parseClassName);
@@ -352,8 +361,8 @@ class ParseUser extends ParseObject implements ParseCloneable {
352361
}
353362

354363
/// Handles all the response data for this class
355-
static ParseResponse _handleResponse(ParseUser user, Response response,
356-
ParseApiRQ type, bool debug, String className) {
364+
static Future<ParseResponse> _handleResponse(ParseUser user, Response response,
365+
ParseApiRQ type, bool debug, String className) async {
357366
final ParseResponse parseResponse =
358367
handleResponse<ParseUser>(user, response, type, debug, className);
359368

@@ -372,7 +381,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
372381
return parseResponse;
373382
} else {
374383
final ParseUser user = parseResponse.result;
375-
user?.saveInStorage(keyParseStoreUser);
384+
await user?._onResponseSuccess();
376385
return parseResponse;
377386
}
378387
}

0 commit comments

Comments
 (0)