Skip to content

Commit 63b1420

Browse files
committed
Add method for batch pointer fetch from LDS.
1 parent 05890ef commit 63b1420

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

Parse/Internal/LocalDataStore/OfflineStore/PFOfflineStore.m

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ - (BFTask *)getOrCreateUUIDAsyncForObject:(PFObject *)object
867867
return tcs.task;
868868
}
869869

870+
#pragma mark Pointers
871+
870872
/*!
871873
Gets an unfetched pointer to an object in the database, based on its uuid. The object may or may
872874
not be in memory, but it must be in database. If it is already in memory, the instance will be
@@ -877,8 +879,7 @@ - (BFTask *)getOrCreateUUIDAsyncForObject:(PFObject *)object
877879
@param database The database instance to retrieve from.
878880
@returns The object with that UUID.
879881
*/
880-
- (BFTask *)_getPointerAsyncWithUUID:(NSString *)uuid
881-
database:(PFSQLiteDatabase *)database {
882+
- (BFTask *)_getPointerAsyncWithUUID:(NSString *)uuid database:(PFSQLiteDatabase *)database {
882883
@synchronized (self.lock) {
883884
PFObject *existing = [self.UUIDToObjectMap objectForKey:uuid];
884885
if (existing) {
@@ -905,24 +906,70 @@ - (BFTask *)_getPointerAsyncWithUUID:(NSString *)uuid
905906

906907
return nil;
907908
}] continueWithSuccessBlock:^id(BFTask *_) {
908-
PFObject *pointer = nil;
909-
@synchronized (self.lock) {
910-
pointer = [self.UUIDToObjectMap objectForKey:uuid];
911-
if (!pointer) {
912-
pointer = [PFObject objectWithoutDataWithClassName:className objectId:objectId];
913-
914-
// If it doesn't have objectId, we don't really need the UUID, and this simplifies some
915-
// other logic elsewhere if we only update the map for new objects.
916-
if (!objectId) {
917-
[self.UUIDToObjectMap setObject:pointer forKey:uuid];
918-
[self.objectToUUIDMap setObject:[BFTask taskWithResult:uuid] forKey:pointer];
919-
}
909+
return [self _getOrCreateInMemoryPointerForObjectWithUUID:uuid parseClassName:className objectId:objectId];
910+
}];
911+
}
912+
913+
- (BFTask PF_GENERIC(NSArray<PFObject *> *)*)_getObjectPointersAsyncWithUUIDs:(NSArray PF_GENERIC(NSString *)*)uuids
914+
fromDatabase:(PFSQLiteDatabase *)database {
915+
NSMutableArray PF_GENERIC(PFObject *)*objects = [NSMutableArray array];
916+
NSMutableArray PF_GENERIC(NSString *)*missingUUIDs = [NSMutableArray array];
917+
@synchronized(self.lock) {
918+
for (NSString *uuid in uuids) {
919+
PFObject *object = [self.UUIDToObjectMap objectForKey:uuid];
920+
if (object) {
921+
[objects addObject:object];
922+
} else {
923+
[missingUUIDs addObject:uuid];
920924
}
921925
}
922-
return pointer;
926+
}
927+
NSString *queryString = [NSString stringWithFormat:@"SELECT %@, %@, %@ FROM %@ WHERE %@ IN ('%@');",
928+
PFOfflineStoreKeyOfUUID, PFOfflineStoreKeyOfObjectId, PFOfflineStoreKeyOfClassName,
929+
PFOfflineStoreTableOfObjects, PFOfflineStoreKeyOfUUID,
930+
[missingUUIDs componentsJoinedByString:@"','"]];
931+
NSMutableArray PF_GENERIC(BFTask <PFObject *>*)*fetchPointersTasks = [NSMutableArray arrayWithCapacity:missingUUIDs.count];
932+
return [[database executeQueryAsync:queryString withArgumentsInArray:nil block:^id(PFSQLiteDatabaseResult *result) {
933+
while ([result next]) {
934+
NSString *uuid = [result stringForColumnIndex:0];
935+
NSString *objectId = [result stringForColumnIndex:1];
936+
NSString *parseClassName = [result stringForColumnIndex:2];
937+
BFTask *task = [BFTask taskFromExecutor:[BFExecutor defaultPriorityBackgroundExecutor] withBlock:^id{
938+
return [self _getOrCreateInMemoryPointerForObjectWithUUID:uuid parseClassName:parseClassName objectId:objectId];
939+
}];
940+
[fetchPointersTasks addObject:task];
941+
}
942+
return [BFTask taskForCompletionOfAllTasks:fetchPointersTasks];
943+
}] continueWithSuccessBlock:^id(BFTask *_) {
944+
for (BFTask PF_GENERIC(PFObject *)*task in fetchPointersTasks) {
945+
[objects addObject:task.result];
946+
}
947+
return objects;
923948
}];
924949
}
925950

951+
- (BFTask PF_GENERIC(PFObject *)*)_getOrCreateInMemoryPointerForObjectWithUUID:(NSString *)uuid
952+
parseClassName:(NSString *)parseClassName
953+
objectId:(NSString *)objectId {
954+
PFObject *pointer = nil;
955+
@synchronized (self.lock) {
956+
pointer = [self.UUIDToObjectMap objectForKey:uuid];
957+
if (!pointer) {
958+
pointer = [PFObject objectWithoutDataWithClassName:parseClassName objectId:objectId];
959+
960+
// If it doesn't have objectId, we don't really need the UUID, and this simplifies some
961+
// other logic elsewhere if we only update the map for new objects.
962+
if (!objectId) {
963+
[self.UUIDToObjectMap setObject:pointer forKey:uuid];
964+
[self.objectToUUIDMap setObject:[BFTask taskWithResult:uuid] forKey:pointer];
965+
}
966+
}
967+
}
968+
return [BFTask taskWithResult:pointer];
969+
}
970+
971+
#pragma mark Else
972+
926973
- (PFObject *)getOrCreateObjectWithoutDataWithClassName:(NSString *)className
927974
objectId:(NSString *)objectId {
928975
PFParameterAssert(objectId, @"objectId cannot be nil.");

0 commit comments

Comments
 (0)