Skip to content

Commit e756eab

Browse files
committed
Fix: Multiple "current" ParseInstallation instances being created
This fixes a race condition where multiple `ParseInstallation` instances can be returned as "current".
1 parent 0966ee3 commit e756eab

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

Parse/src/main/java/com/parse/CachedCurrentInstallationController.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,10 @@ public Task<Void> then(Task<Void> task) throws Exception {
7070

7171
@Override
7272
public Task<ParseInstallation> getAsync() {
73-
final ParseInstallation cachedCurrent;
7473
synchronized (mutex) {
75-
cachedCurrent = currentInstallation;
76-
}
77-
78-
if (cachedCurrent != null) {
79-
return Task.forResult(cachedCurrent);
74+
if (currentInstallation != null) {
75+
return Task.forResult(currentInstallation);
76+
}
8077
}
8178

8279
return taskQueue.enqueue(new Continuation<Void, Task<ParseInstallation>>() {
@@ -85,26 +82,32 @@ public Task<ParseInstallation> then(Task<Void> toAwait) throws Exception {
8582
return toAwait.continueWithTask(new Continuation<Void, Task<ParseInstallation>>() {
8683
@Override
8784
public Task<ParseInstallation> then(Task<Void> task) throws Exception {
88-
return store.getAsync();
89-
}
90-
}).continueWith(new Continuation<ParseInstallation, ParseInstallation>() {
91-
@Override
92-
public ParseInstallation then(Task<ParseInstallation> task) throws Exception {
93-
ParseInstallation current = task.getResult();
94-
if (current == null) {
95-
current = ParseObject.create(ParseInstallation.class);
96-
current.updateDeviceInfo(installationId);
97-
} else {
98-
installationId.set(current.getInstallationId());
99-
PLog.v(TAG, "Successfully deserialized Installation object");
100-
}
101-
10285
synchronized (mutex) {
103-
currentInstallation = current;
86+
if (currentInstallation != null) {
87+
return Task.forResult(currentInstallation);
88+
}
10489
}
105-
return current;
90+
91+
return store.getAsync().continueWith(new Continuation<ParseInstallation, ParseInstallation>() {
92+
@Override
93+
public ParseInstallation then(Task<ParseInstallation> task) throws Exception {
94+
ParseInstallation current = task.getResult();
95+
if (current == null) {
96+
current = ParseObject.create(ParseInstallation.class);
97+
current.updateDeviceInfo(installationId);
98+
} else {
99+
installationId.set(current.getInstallationId());
100+
PLog.v(TAG, "Successfully deserialized Installation object");
101+
}
102+
103+
synchronized (mutex) {
104+
currentInstallation = current;
105+
}
106+
return current;
107+
}
108+
}, ParseExecutors.io());
106109
}
107-
}, ParseExecutors.io());
110+
});
108111
}
109112
});
110113
}

0 commit comments

Comments
 (0)