1- part of '../../parse_server_sdk.dart' ;
1+ part of '../../parse_server_sdk.dart' ;
22
33extension ParseObjectOffline on ParseObject {
44 /// Load a single object by objectId from local storage.
5- static Future <ParseObject ?> loadFromLocalCache (String className, String objectId) async {
5+ static Future <ParseObject ?> loadFromLocalCache (
6+ String className,
7+ String objectId,
8+ ) async {
69 final CoreStore coreStore = ParseCoreData ().getStore ();
710 final String cacheKey = 'offline_cache_$className ' ;
8- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
11+ final List <String > cached = await _getStringListAsStrings (
12+ coreStore,
13+ cacheKey,
14+ );
915 for (final s in cached) {
1016 final jsonObj = json.decode (s);
1117 if (jsonObj['objectId' ] == objectId) {
@@ -20,24 +26,35 @@ extension ParseObjectOffline on ParseObject {
2026 Future <void > saveToLocalCache () async {
2127 final CoreStore coreStore = ParseCoreData ().getStore ();
2228 final String cacheKey = 'offline_cache_$parseClassName ' ;
23- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
29+ final List <String > cached = await _getStringListAsStrings (
30+ coreStore,
31+ cacheKey,
32+ );
2433 // Remove any existing object with the same objectId
2534 cached.removeWhere ((s) {
2635 final jsonObj = json.decode (s);
2736 return jsonObj['objectId' ] == objectId;
2837 });
2938 cached.add (json.encode (toJson (full: true )));
3039 await coreStore.setStringList (cacheKey, cached);
31- print ('Saved object ${objectId ?? "(no objectId)" } to local cache for $parseClassName ' );
40+ print (
41+ 'Saved object ${objectId ?? "(no objectId)" } to local cache for $parseClassName ' ,
42+ );
3243 }
3344
3445 /// Save a list of objects to local storage efficiently.
35- static Future <void > saveAllToLocalCache (String className, List <ParseObject > objectsToSave) async {
46+ static Future <void > saveAllToLocalCache (
47+ String className,
48+ List <ParseObject > objectsToSave,
49+ ) async {
3650 if (objectsToSave.isEmpty) return ;
3751
3852 final CoreStore coreStore = ParseCoreData ().getStore ();
3953 final String cacheKey = 'offline_cache_$className ' ;
40- final List <String > cachedStrings = await _getStringListAsStrings (coreStore, cacheKey);
54+ final List <String > cachedStrings = await _getStringListAsStrings (
55+ coreStore,
56+ cacheKey,
57+ );
4158
4259 // Use a Map for efficient lookup and update of existing objects
4360 final Map <String , String > objectMap = {};
@@ -49,7 +66,7 @@ extension ParseObjectOffline on ParseObject {
4966 objectMap[objectId] = s; // Store the original JSON string
5067 }
5168 } catch (e) {
52- print ('Error decoding cached object string during batch save: $e ' );
69+ print ('Error decoding cached object string during batch save: $e ' );
5370 }
5471 }
5572
@@ -68,34 +85,48 @@ extension ParseObjectOffline on ParseObject {
6885 // Encode the new object data and replace/add it in the map
6986 objectMap[objectId] = json.encode (obj.toJson (full: true ));
7087 } else {
71- print ('Skipping object without objectId during batch save for $className ' );
88+ print (
89+ 'Skipping object without objectId during batch save for $className ' ,
90+ );
7291 }
7392 }
7493
7594 // Convert the map values back to a list and save
7695 final List <String > updatedCachedStrings = objectMap.values.toList ();
7796 await coreStore.setStringList (cacheKey, updatedCachedStrings);
78- print ('Batch saved to local cache for $className . Added: $added , Updated: $updated , Total: ${updatedCachedStrings .length }' );
97+ print (
98+ 'Batch saved to local cache for $className . Added: $added , Updated: $updated , Total: ${updatedCachedStrings .length }' ,
99+ );
79100 }
80101
81102 /// Remove this object from local storage (CoreStore).
82103 Future <void > removeFromLocalCache () async {
83104 final CoreStore coreStore = ParseCoreData ().getStore ();
84105 final String cacheKey = 'offline_cache_$parseClassName ' ;
85- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
106+ final List <String > cached = await _getStringListAsStrings (
107+ coreStore,
108+ cacheKey,
109+ );
86110 cached.removeWhere ((s) {
87111 final jsonObj = json.decode (s);
88112 return jsonObj['objectId' ] == objectId;
89113 });
90114 await coreStore.setStringList (cacheKey, cached);
91- print ('Removed object ${objectId ?? "(no objectId)" } from local cache for $parseClassName ' );
115+ print (
116+ 'Removed object ${objectId ?? "(no objectId)" } from local cache for $parseClassName ' ,
117+ );
92118 }
93119
94120 /// Load all objects of this class from local storage.
95- static Future <List <ParseObject >> loadAllFromLocalCache (String className) async {
121+ static Future <List <ParseObject >> loadAllFromLocalCache (
122+ String className,
123+ ) async {
96124 final CoreStore coreStore = ParseCoreData ().getStore ();
97125 final String cacheKey = 'offline_cache_$className ' ;
98- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
126+ final List <String > cached = await _getStringListAsStrings (
127+ coreStore,
128+ cacheKey,
129+ );
99130 print ('Loaded ${cached .length } objects from local cache for $className ' );
100131 return cached.map <ParseObject >((s) {
101132 final jsonObj = json.decode (s);
@@ -106,7 +137,10 @@ extension ParseObjectOffline on ParseObject {
106137 Future <void > updateInLocalCache (Map <String , dynamic > updates) async {
107138 final CoreStore coreStore = ParseCoreData ().getStore ();
108139 final String cacheKey = 'offline_cache_$parseClassName ' ;
109- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
140+ final List <String > cached = await _getStringListAsStrings (
141+ coreStore,
142+ cacheKey,
143+ );
110144 for (int i = 0 ; i < cached.length; i++ ) {
111145 final jsonObj = json.decode (cached[i]);
112146 if (jsonObj['objectId' ] == objectId) {
@@ -116,7 +150,9 @@ extension ParseObjectOffline on ParseObject {
116150 }
117151 }
118152 await coreStore.setStringList (cacheKey, cached);
119- print ('Updated object ${objectId ?? "(no objectId)" } in local cache for $parseClassName ' );
153+ print (
154+ 'Updated object ${objectId ?? "(no objectId)" } in local cache for $parseClassName ' ,
155+ );
120156 }
121157
122158 static Future <void > clearLocalCacheForClass (String className) async {
@@ -126,10 +162,16 @@ extension ParseObjectOffline on ParseObject {
126162 print ('Cleared local cache for $className ' );
127163 }
128164
129- static Future <bool > existsInLocalCache (String className, String objectId) async {
165+ static Future <bool > existsInLocalCache (
166+ String className,
167+ String objectId,
168+ ) async {
130169 final CoreStore coreStore = ParseCoreData ().getStore ();
131170 final String cacheKey = 'offline_cache_$className ' ;
132- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
171+ final List <String > cached = await _getStringListAsStrings (
172+ coreStore,
173+ cacheKey,
174+ );
133175 for (final s in cached) {
134176 final jsonObj = json.decode (s);
135177 if (jsonObj['objectId' ] == objectId) {
@@ -141,10 +183,15 @@ extension ParseObjectOffline on ParseObject {
141183 return false ;
142184 }
143185
144- static Future <List <String >> getAllObjectIdsInLocalCache (String className) async {
186+ static Future <List <String >> getAllObjectIdsInLocalCache (
187+ String className,
188+ ) async {
145189 final CoreStore coreStore = ParseCoreData ().getStore ();
146190 final String cacheKey = 'offline_cache_$className ' ;
147- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
191+ final List <String > cached = await _getStringListAsStrings (
192+ coreStore,
193+ cacheKey,
194+ );
148195 print ('Fetched all objectIds from local cache for $className ' );
149196 return cached.map ((s) => json.decode (s)['objectId' ] as String ).toList ();
150197 }
@@ -157,9 +204,12 @@ extension ParseObjectOffline on ParseObject {
157204 print ('Synced local cache with server for $className ' );
158205 }
159206
160- static Future <List <String >> _getStringListAsStrings (CoreStore coreStore, String cacheKey) async {
207+ static Future <List <String >> _getStringListAsStrings (
208+ CoreStore coreStore,
209+ String cacheKey,
210+ ) async {
161211 final rawList = await coreStore.getStringList (cacheKey);
162212 if (rawList == null ) return [];
163213 return List <String >.from (rawList.map ((e) => e.toString ()));
164214 }
165- }
215+ }
0 commit comments