11part of '../../parse_server_sdk.dart' ;
22
3-
4-
53extension ParseObjectOffline on ParseObject {
6-
7-
84 /// Load a single object by objectId from local storage.
95 static Future <ParseObject ?> loadFromLocalCache (String className, String objectId) async {
106 final CoreStore coreStore = ParseCoreData ().getStore ();
@@ -13,6 +9,7 @@ extension ParseObjectOffline on ParseObject {
139 for (final s in cached) {
1410 final jsonObj = json.decode (s);
1511 if (jsonObj['objectId' ] == objectId) {
12+ print ('Loaded object $objectId from local cache for $className ' );
1613 return ParseObject (className).fromJson (jsonObj);
1714 }
1815 }
@@ -31,9 +28,10 @@ extension ParseObjectOffline on ParseObject {
3128 });
3229 cached.add (json.encode (toJson (full: true )));
3330 await coreStore.setStringList (cacheKey, cached);
31+ print ('Saved object ${objectId ?? "(no objectId)" } to local cache for $parseClassName ' );
3432 }
3533
36- /// Remove this object from local storage (CoreStore).
34+ /// Remove this object from local storage (CoreStore).
3735 Future <void > removeFromLocalCache () async {
3836 final CoreStore coreStore = ParseCoreData ().getStore ();
3937 final String cacheKey = 'offline_cache_${parseClassName }' ;
@@ -43,20 +41,21 @@ extension ParseObjectOffline on ParseObject {
4341 return jsonObj['objectId' ] == objectId;
4442 });
4543 await coreStore.setStringList (cacheKey, cached);
44+ print ('Removed object ${objectId ?? "(no objectId)" } from local cache for $parseClassName ' );
4645 }
4746
4847 /// Load all objects of this class from local storage.
4948 static Future <List <ParseObject >> loadAllFromLocalCache (String className) async {
5049 final CoreStore coreStore = ParseCoreData ().getStore ();
5150 final String cacheKey = 'offline_cache_$className ' ;
52- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
51+ final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
52+ print ('Loaded ${cached .length } objects from local cache for $className ' );
5353 return cached.map <ParseObject >((s) {
5454 final jsonObj = json.decode (s);
5555 return ParseObject (className).fromJson (jsonObj);
5656 }).toList ();
5757 }
5858
59-
6059 Future <void > updateInLocalCache (Map <String , dynamic > updates) async {
6160 final CoreStore coreStore = ParseCoreData ().getStore ();
6261 final String cacheKey = 'offline_cache_${parseClassName }' ;
@@ -70,54 +69,50 @@ extension ParseObjectOffline on ParseObject {
7069 }
7170 }
7271 await coreStore.setStringList (cacheKey, cached);
72+ print ('Updated object ${objectId ?? "(no objectId)" } in local cache for $parseClassName ' );
7373 }
7474
7575 static Future <void > clearLocalCacheForClass (String className) async {
76- final CoreStore coreStore = ParseCoreData ().getStore ();
77- final String cacheKey = 'offline_cache_$className ' ;
78- await coreStore.setStringList (cacheKey, []);
79- }
76+ final CoreStore coreStore = ParseCoreData ().getStore ();
77+ final String cacheKey = 'offline_cache_$className ' ;
78+ await coreStore.setStringList (cacheKey, []);
79+ print ('Cleared local cache for $className ' );
80+ }
8081
81- static Future <bool > existsInLocalCache (String className, String objectId) async {
82- final CoreStore coreStore = ParseCoreData ().getStore ();
83- final String cacheKey = 'offline_cache_$className ' ;
84- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
85- for (final s in cached) {
86- final jsonObj = json.decode (s);
87- if (jsonObj['objectId' ] == objectId) {
88- return true ;
82+ static Future <bool > existsInLocalCache (String className, String objectId) async {
83+ final CoreStore coreStore = ParseCoreData ().getStore ();
84+ final String cacheKey = 'offline_cache_$className ' ;
85+ final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
86+ for (final s in cached) {
87+ final jsonObj = json.decode (s);
88+ if (jsonObj['objectId' ] == objectId) {
89+ print ('Object $objectId exists in local cache for $className ' );
90+ return true ;
91+ }
8992 }
93+ print ('Object $objectId does not exist in local cache for $className ' );
94+ return false ;
9095 }
91- return false ;
92- }
93- static Future <List <String >> getAllObjectIdsInLocalCache (String className) async {
94- final CoreStore coreStore = ParseCoreData ().getStore ();
95- final String cacheKey = 'offline_cache_$className ' ;
96- final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
97- return cached.map ((s) => json.decode (s)['objectId' ] as String ).toList ();
98- }
9996
100- static Future <void > syncLocalCacheWithServer (String className) async {
101- final objects = await loadAllFromLocalCache (className);
102- for (final obj in objects) {
103- await obj.save ();
97+ static Future <List <String >> getAllObjectIdsInLocalCache (String className) async {
98+ final CoreStore coreStore = ParseCoreData ().getStore ();
99+ final String cacheKey = 'offline_cache_$className ' ;
100+ final List <String > cached = await _getStringListAsStrings (coreStore, cacheKey);
101+ print ('Fetched all objectIds from local cache for $className ' );
102+ return cached.map ((s) => json.decode (s)['objectId' ] as String ).toList ();
104103 }
105- }
106104
107- static Future <List <String >> _getStringListAsStrings (CoreStore coreStore, String cacheKey) async {
105+ static Future <void > syncLocalCacheWithServer (String className) async {
106+ final objects = await loadAllFromLocalCache (className);
107+ for (final obj in objects) {
108+ await obj.save ();
109+ }
110+ print ('Synced local cache with server for $className ' );
111+ }
108112
109- // final rawList = await coreStore.getStringList(cacheKey);
110- // final List<String> cached = [];
111- // if (rawList != null) {
112- // for (final e in rawList) {
113- // cached.add(e.toString());
114- // }
115- // }
113+ static Future <List <String >> _getStringListAsStrings (CoreStore coreStore, String cacheKey) async {
116114 final rawList = await coreStore.getStringList (cacheKey);
117115 if (rawList == null ) return [];
118116 return List <String >.from (rawList.map ((e) => e.toString ()));
119117 }
120- }
121-
122- // await object.saveToLocalCache();
123- // final offlineObjects = await ParseObjectOffline.loadAllFromLocalCache('YourClassName');
118+ }
0 commit comments