Loading...
Searching...
No Matches
StorageManagerExample.m
Go to the documentation of this file.
2
3@interface StorageManagerExample ()
4@property (nonatomic, strong) NavigineSdk *sdk;
5@property (nonatomic, strong) StorageManager *storageManager;
6@property (nonatomic, strong) KeyValueStorage *userStorage;
7@property (nonatomic, strong) KeyValueStorage *appStorage;
8@property (nonatomic, strong) KeyValueStorage *cacheStorage;
9@end
10
11@implementation StorageManagerExample
12
13- (instancetype)init {
14 self = [super init];
15 if (self) {
16 [self initializeSdk];
17 [self setupStorages];
18 }
19 return self;
20}
21
25- (void)initializeSdk {
26 @try {
27 // [objc_NavigineSdk_getInstance]
28 // Get SDK instance
29 self.sdk = [NavigineSdk getInstance];
30 // [objc_NavigineSdk_getInstance]
31
32 // [objc_NavigineSdk_setUserHash]
33 // Set user hash
34 [self.sdk setUserHash:@"USER-HASH-HERE"];
35 // [objc_NavigineSdk_setUserHash]
36
37 // [objc_NavigineSdk_setServer]
38 // Set server URL (optional)
39 [self.sdk setServer:@"https://custom.navigine.com"];
40 // [objc_NavigineSdk_setServer]
41
42 // [objc_NavigineSdk_getStorageManager]
43 // Get StorageManager for working with storages
44 self.storageManager = [self.sdk getStorageManager];
45 // [objc_NavigineSdk_getStorageManager]
46
47 if (self.storageManager != nil) {
48 NSLog(@"StorageManager successfully initialized");
49 }
50 } @catch (NSException *exception) {
51 NSLog(@"Error initializing SDK: %@", exception.reason);
52 }
53}
54
58- (void)setupStorages {
59 if (self.storageManager == nil) {
60 NSLog(@"StorageManager not initialized");
61 return;
62 }
63
64 // [objc_StorageManager_getStorage]
65 // Get or create different storage instances
66 self.userStorage = [self.storageManager getStorage:@"user_preferences"];
67 self.appStorage = [self.storageManager getStorage:@"app_settings"];
68 self.cacheStorage = [self.storageManager getStorage:@"cache"];
69 // [objc_StorageManager_getStorage]
70
71 NSLog(@"Storage instances created");
72}
73
77- (void)demonstrateStorageManagerMethods {
78 if (self.storageManager == nil) {
79 NSLog(@"StorageManager not initialized");
80 return;
81 }
82
83 // [objc_StorageManager_getStorageList]
84 // Get list of all existing storages
85 NSArray<NSString *> *storageList = [self.storageManager getStorageList];
86 NSLog(@"Existing storages: %@", storageList);
87 // [objc_StorageManager_getStorageList]
88
89 // [objc_StorageManager_getStorage1]
90 // Get or create storage by name
91 KeyValueStorage *testStorage = [self.storageManager getStorage:@"test_storage"];
92 NSLog(@"Created test storage");
93 // [objc_StorageManager_getStorage1]
94
95 // Demonstrate storage creation and listing
96 [self.storageManager getStorage:@"another_storage"];
97 NSArray<NSString *> *updatedList = [self.storageManager getStorageList];
98 NSLog(@"Updated storage list: %@", updatedList);
99
100 // [objc_StorageManager_removeStorage]
101 // Remove storage and all its data
102 [self.storageManager removeStorage:@"test_storage"];
103 NSLog(@"Removed test storage");
104 // [objc_StorageManager_removeStorage]
105
106 NSArray<NSString *> *finalList = [self.storageManager getStorageList];
107 NSLog(@"Final storage list: %@", finalList);
108}
109
113- (void)demonstrateKeyValueStorageMethods {
114 if (self.userStorage == nil) {
115 NSLog(@"User storage not initialized");
116 return;
117 }
118
119 NSLog(@"\n=== KeyValueStorage Methods Demonstration ===");
120
121 // [objc_KeyValueStorage_putString]
122 // Store string values
123 [self.userStorage putString:@"user_name" value:@"John Doe"];
124 [self.userStorage putString:@"user_email" value:@"john.doe@example.com"];
125 NSLog(@"Stored string values");
126 // [objc_KeyValueStorage_putString]
127
128 // [objc_KeyValueStorage_putInt]
129 // Store integer values
130 [self.userStorage putInt:@"user_age" value:25];
131 [self.userStorage putInt:@"login_count" value:42];
132 NSLog(@"Stored integer values");
133 // [objc_KeyValueStorage_putInt]
134
135 // [objc_KeyValueStorage_putLong]
136 // Store long values
137 [self.userStorage putLong:@"registration_timestamp" value:1640995200000LL];
138 [self.userStorage putLong:@"last_login_timestamp" value:(long long)[[NSDate date] timeIntervalSince1970] * 1000];
139 NSLog(@"Stored long values");
140 // [objc_KeyValueStorage_putLong]
141
142 // [objc_KeyValueStorage_putBool]
143 // Store boolean values
144 [self.userStorage putBool:@"is_premium" value:YES];
145 [self.userStorage putBool:@"notifications_enabled" value:NO];
146 NSLog(@"Stored boolean values");
147 // [objc_KeyValueStorage_putBool]
148
149 // [objc_KeyValueStorage_putFloat]
150 // Store float values
151 [self.userStorage putFloat:@"user_rating" value:4.5f];
152 [self.userStorage putFloat:@"temperature" value:23.5f];
153 NSLog(@"Stored float values");
154 // [objc_KeyValueStorage_putFloat]
155
156 // [objc_KeyValueStorage_putDouble]
157 // Store double values
158 [self.userStorage putDouble:@"user_location_lat" value:55.7558];
159 [self.userStorage putDouble:@"user_location_lng" value:37.6176];
160 NSLog(@"Stored double values");
161 // [objc_KeyValueStorage_putDouble]
162
163 // [objc_KeyValueStorage_contains]
164 // Check if keys exist
165 BOOL hasUserName = [self.userStorage contains:@"user_name"];
166 BOOL hasNonExistent = [self.userStorage contains:@"non_existent_key"];
167 NSLog(@"Contains 'user_name': %@", hasUserName ? @"YES" : @"NO");
168 NSLog(@"Contains 'non_existent_key': %@", hasNonExistent ? @"YES" : @"NO");
169 // [objc_KeyValueStorage_contains]
170
171 // [objc_KeyValueStorage_getKeys]
172 // Get all stored keys
173 NSArray<NSString *> *allKeys = [self.userStorage getKeys];
174 NSLog(@"All stored keys: %@", allKeys);
175 // [objc_KeyValueStorage_getKeys]
176
177 // [objc_KeyValueStorage_getString]
178 // Retrieve string values with defaults
179 NSString *userName = [self.userStorage getString:@"user_name" defaultValue:@"Unknown"];
180 NSString *userEmail = [self.userStorage getString:@"user_email" defaultValue:@""];
181 NSString *nonExistent = [self.userStorage getString:@"non_existent_key" defaultValue:@"default_value"];
182 NSLog(@"User name: %@", userName);
183 NSLog(@"User email: %@", userEmail);
184 NSLog(@"Non-existent key: %@", nonExistent);
185 // [objc_KeyValueStorage_getString]
186
187 // [objc_KeyValueStorage_getInt]
188 // Retrieve integer values with defaults
189 int32_t userAge = [self.userStorage getInt:@"user_age" defaultValue:0];
190 int32_t loginCount = [self.userStorage getInt:@"login_count" defaultValue:0];
191 int32_t nonExistentInt = [self.userStorage getInt:@"non_existent_int" defaultValue:-1];
192 NSLog(@"User age: %d", userAge);
193 NSLog(@"Login count: %d", loginCount);
194 NSLog(@"Non-existent int: %d", nonExistentInt);
195 // [objc_KeyValueStorage_getInt]
196
197 // [objc_KeyValueStorage_getLong]
198 // Retrieve long values with defaults
199 int64_t regTimestamp = [self.userStorage getLong:@"registration_timestamp" defaultValue:0];
200 int64_t lastLogin = [self.userStorage getLong:@"last_login_timestamp" defaultValue:0];
201 int64_t nonExistentLong = [self.userStorage getLong:@"non_existent_long" defaultValue:-1];
202 NSLog(@"Registration timestamp: %lld", regTimestamp);
203 NSLog(@"Last login timestamp: %lld", lastLogin);
204 NSLog(@"Non-existent long: %lld", nonExistentLong);
205 // [objc_KeyValueStorage_getLong]
206
207 // [objc_KeyValueStorage_getBool]
208 // Retrieve boolean values with defaults
209 BOOL isPremium = [self.userStorage getBool:@"is_premium" defaultValue:NO];
210 BOOL notificationsEnabled = [self.userStorage getBool:@"notifications_enabled" defaultValue:YES];
211 BOOL nonExistentBool = [self.userStorage getBool:@"non_existent_bool" defaultValue:NO];
212 NSLog(@"Is premium: %@", isPremium ? @"YES" : @"NO");
213 NSLog(@"Notifications enabled: %@", notificationsEnabled ? @"YES" : @"NO");
214 NSLog(@"Non-existent bool: %@", nonExistentBool ? @"YES" : @"NO");
215 // [objc_KeyValueStorage_getBool]
216
217 // [objc_KeyValueStorage_getFloat]
218 // Retrieve float values with defaults
219 float userRating = [self.userStorage getFloat:@"user_rating" defaultValue:0.0f];
220 float temperature = [self.userStorage getFloat:@"temperature" defaultValue:0.0f];
221 float nonExistentFloat = [self.userStorage getFloat:@"non_existent_float" defaultValue:-1.0f];
222 NSLog(@"User rating: %f", userRating);
223 NSLog(@"Temperature: %f", temperature);
224 NSLog(@"Non-existent float: %f", nonExistentFloat);
225 // [objc_KeyValueStorage_getFloat]
226
227 // [objc_KeyValueStorage_getDouble]
228 // Retrieve double values with defaults
229 double userLat = [self.userStorage getDouble:@"user_location_lat" defaultValue:0.0];
230 double userLng = [self.userStorage getDouble:@"user_location_lng" defaultValue:0.0];
231 double nonExistentDouble = [self.userStorage getDouble:@"non_existent_double" defaultValue:-1.0];
232 NSLog(@"User location lat: %f", userLat);
233 NSLog(@"User location lng: %f", userLng);
234 NSLog(@"Non-existent double: %f", nonExistentDouble);
235 // [objc_KeyValueStorage_getDouble]
236
237 // [objc_KeyValueStorage_remove]
238 // Remove specific keys
239 [self.userStorage remove:@"user_age"];
240 [self.userStorage remove:@"non_existent_key"]; // No-op
241 NSLog(@"Removed 'user_age' key");
242 // [objc_KeyValueStorage_remove]
243
244 // Verify removal
245 BOOL stillHasAge = [self.userStorage contains:@"user_age"];
246 NSLog(@"Still contains 'user_age': %@", stillHasAge ? @"YES" : @"NO");
247
248 // [objc_KeyValueStorage_clear]
249 // Clear all data
250 [self.userStorage clear];
251 NSLog(@"Cleared all data from user storage");
252 // [objc_KeyValueStorage_clear]
253
254 // Verify clear
255 NSArray<NSString *> *keysAfterClear = [self.userStorage getKeys];
256 NSLog(@"Keys after clear: %@", keysAfterClear);
257}
258
262- (void)demonstrateMultipleStorages {
263 if (self.userStorage == nil || self.appStorage == nil || self.cacheStorage == nil) {
264 NSLog(@"Storages not initialized");
265 return;
266 }
267
268 NSLog(@"\n=== Multiple Storages Demonstration ===");
269
270 // Store different data in different storages
271 [self.userStorage putString:@"name" value:@"John Doe"];
272 [self.userStorage putInt:@"age" value:25];
273 [self.userStorage putBool:@"is_premium" value:YES];
274
275 [self.appStorage putString:@"theme" value:@"dark"];
276 [self.appStorage putInt:@"api_version" value:2];
277 [self.appStorage putBool:@"debug_mode" value:NO];
278
279 [self.cacheStorage putLong:@"last_update" value:(long long)[[NSDate date] timeIntervalSince1970] * 1000];
280 [self.cacheStorage putString:@"api_token" value:@"abc123"];
281 [self.cacheStorage putDouble:@"cached_rating" value:4.2];
282
283 // Retrieve data from different storages
284 NSLog(@"User storage data:");
285 [self demonstrateStorageData:self.userStorage];
286
287 NSLog(@"\nApp storage data:");
288 [self demonstrateStorageData:self.appStorage];
289
290 NSLog(@"\nCache storage data:");
291 [self demonstrateStorageData:self.cacheStorage];
292}
293
297- (void)demonstrateStorageData:(KeyValueStorage *)storage {
298 NSArray<NSString *> *keys = [storage getKeys];
299 for (NSString *key in keys) {
300 if ([storage contains:key]) {
301 // Try to get value as string first
302 NSString *stringValue = [storage getString:key defaultValue:@""];
303 if (stringValue.length > 0) {
304 NSLog(@" %@: %@ (string)", key, stringValue);
305 continue;
306 }
307
308 // Try integer
309 int32_t intValue = [storage getInt:key defaultValue:-999999];
310 if (intValue != -999999) {
311 NSLog(@" %@: %d (int)", key, intValue);
312 continue;
313 }
314
315 // Try long
316 int64_t longValue = [storage getLong:key defaultValue:-999999];
317 if (longValue != -999999) {
318 NSLog(@" %@: %lld (long)", key, longValue);
319 continue;
320 }
321
322 // Try boolean
323 BOOL boolValue = [storage getBool:key defaultValue:NO];
324 if ([storage contains:key]) {
325 NSLog(@" %@: %@ (bool)", key, boolValue ? @"YES" : @"NO");
326 continue;
327 }
328
329 // Try float
330 float floatValue = [storage getFloat:key defaultValue:-999999.0f];
331 if (floatValue != -999999.0f) {
332 NSLog(@" %@: %f (float)", key, floatValue);
333 continue;
334 }
335
336 // Try double
337 double doubleValue = [storage getDouble:key defaultValue:-999999.0];
338 if (doubleValue != -999999.0) {
339 NSLog(@" %@: %f (double)", key, doubleValue);
340 }
341 }
342 }
343}
344
348- (void)demonstrateStorageAnalytics {
349 if (self.storageManager == nil) {
350 NSLog(@"StorageManager not initialized");
351 return;
352 }
353
354 NSLog(@"\n=== Storage Analytics ===");
355
356 // Analyze all storages
357 NSArray<NSString *> *allStorages = [self.storageManager getStorageList];
358 NSLog(@"Total storages: %lu", (unsigned long)allStorages.count);
359
360 for (NSString *storageName in allStorages) {
361 KeyValueStorage *storage = [self.storageManager getStorage:storageName];
362 NSArray<NSString *> *keys = [storage getKeys];
363 NSLog(@"Storage '%@' contains %lu items", storageName, (unsigned long)keys.count);
364
365 // Analyze data types
366 int stringCount = 0;
367 int intCount = 0;
368 int longCount = 0;
369 int boolCount = 0;
370 int floatCount = 0;
371 int doubleCount = 0;
372
373 for (NSString *key in keys) {
374 // Count by trying to get each type
375 NSString *stringValue = [storage getString:key defaultValue:@""];
376 if (stringValue.length > 0) {
377 stringCount++;
378 continue;
379 }
380
381 int32_t intValue = [storage getInt:key defaultValue:-999999];
382 if (intValue != -999999) {
383 intCount++;
384 continue;
385 }
386
387 int64_t longValue = [storage getLong:key defaultValue:-999999];
388 if (longValue != -999999) {
389 longCount++;
390 continue;
391 }
392
393 BOOL boolValue = [storage getBool:key defaultValue:NO];
394 if ([storage contains:key]) {
395 boolCount++;
396 continue;
397 }
398
399 float floatValue = [storage getFloat:key defaultValue:-999999.0f];
400 if (floatValue != -999999.0f) {
401 floatCount++;
402 continue;
403 }
404
405 double doubleValue = [storage getDouble:key defaultValue:-999999.0];
406 if (doubleValue != -999999.0) {
407 doubleCount++;
408 }
409 }
410
411 NSLog(@" - Strings: %d", stringCount);
412 NSLog(@" - Integers: %d", intCount);
413 NSLog(@" - Longs: %d", longCount);
414 NSLog(@" - Booleans: %d", boolCount);
415 NSLog(@" - Floats: %d", floatCount);
416 NSLog(@" - Doubles: %d", doubleCount);
417 }
418}
419
423- (void)demonstrateDataMigration {
424 if (self.storageManager == nil) {
425 NSLog(@"StorageManager not initialized");
426 return;
427 }
428
429 NSLog(@"\n=== Data Migration Demonstration ===");
430
431 // Create old and new storages
432 KeyValueStorage *oldStorage = [self.storageManager getStorage:@"old_storage"];
433 KeyValueStorage *newStorage = [self.storageManager getStorage:@"new_storage"];
434
435 // Populate old storage with data
436 [oldStorage putString:@"name" value:@"John Doe"];
437 [oldStorage putInt:@"age" value:25];
438 [oldStorage putBool:@"is_premium" value:YES];
439 [oldStorage putDouble:@"rating" value:4.5];
440
441 NSLog(@"Old storage before migration:");
442 [self demonstrateStorageData:oldStorage];
443
444 // Migrate data from old to new storage
445 NSArray<NSString *> *keys = [oldStorage getKeys];
446 for (NSString *key in keys) {
447 // Migrate string data
448 NSString *stringValue = [oldStorage getString:key defaultValue:@""];
449 if (stringValue.length > 0) {
450 [newStorage putString:key value:stringValue];
451 continue;
452 }
453
454 // Migrate integer data
455 int32_t intValue = [oldStorage getInt:key defaultValue:-999999];
456 if (intValue != -999999) {
457 [newStorage putInt:key value:intValue];
458 continue;
459 }
460
461 // Migrate long data
462 int64_t longValue = [oldStorage getLong:key defaultValue:-999999];
463 if (longValue != -999999) {
464 [newStorage putLong:key value:longValue];
465 continue;
466 }
467
468 // Migrate boolean data
469 BOOL boolValue = [oldStorage getBool:key defaultValue:NO];
470 if ([oldStorage contains:key]) {
471 [newStorage putBool:key value:boolValue];
472 continue;
473 }
474
475 // Migrate float data
476 float floatValue = [oldStorage getFloat:key defaultValue:-999999.0f];
477 if (floatValue != -999999.0f) {
478 [newStorage putFloat:key value:floatValue];
479 continue;
480 }
481
482 // Migrate double data
483 double doubleValue = [oldStorage getDouble:key defaultValue:-999999.0];
484 if (doubleValue != -999999.0) {
485 [newStorage putDouble:key value:doubleValue];
486 }
487 }
488
489 NSLog(@"\nNew storage after migration:");
490 [self demonstrateStorageData:newStorage];
491
492 // Remove old storage
493 [self.storageManager removeStorage:@"old_storage"];
494 NSLog(@"\nRemoved old storage");
495
496 // Verify migration
497 NSArray<NSString *> *finalStorages = [self.storageManager getStorageList];
498 NSLog(@"Final storage list: %@", finalStorages);
499}
500
504- (void)demonstrateStorageCleanup {
505 if (self.storageManager == nil) {
506 NSLog(@"StorageManager not initialized");
507 return;
508 }
509
510 NSLog(@"\n=== Storage Cleanup Demonstration ===");
511
512 // Create some temporary storages
513 [self.storageManager getStorage:@"temp_cache_1"];
514 [self.storageManager getStorage:@"temp_cache_2"];
515 [self.storageManager getStorage:@"permanent_storage"];
516
517 NSArray<NSString *> *beforeCleanup = [self.storageManager getStorageList];
518 NSLog(@"Storages before cleanup: %@", beforeCleanup);
519
520 // Clean up temporary storages
521 NSArray<NSString *> *storages = [self.storageManager getStorageList];
522 for (NSString *storageName in storages) {
523 if ([storageName hasPrefix:@"temp_"]) {
524 [self.storageManager removeStorage:storageName];
525 NSLog(@"Removed temporary storage: %@", storageName);
526 }
527 }
528
529 NSArray<NSString *> *afterCleanup = [self.storageManager getStorageList];
530 NSLog(@"Storages after cleanup: %@", afterCleanup);
531}
532
536- (void)runAllDemonstrations {
537 NSLog(@"=== StorageManager and KeyValueStorage Examples ===\n");
538
539 [self demonstrateStorageManagerMethods];
540 [self demonstrateKeyValueStorageMethods];
541 [self demonstrateMultipleStorages];
542 [self demonstrateStorageAnalytics];
543 [self demonstrateDataMigration];
544 [self demonstrateStorageCleanup];
545
546 NSLog(@"\n=== All demonstrations completed ===");
547}
548
552- (void)cleanup {
553 if (self.storageManager != nil) {
554 // Remove test storages
555 NSArray<NSString *> *storages = [self.storageManager getStorageList];
556 for (NSString *storageName in storages) {
557 if ([storageName hasPrefix:@"test_"] ||
558 [storageName hasPrefix:@"temp_"] ||
559 [storageName hasPrefix:@"old_"] ||
560 [storageName hasPrefix:@"new_"]) {
561 [self.storageManager removeStorage:storageName];
562 }
563 }
564 }
565}
566
567@end
568
572int main(int argc, const char * argv[]) {
573 @autoreleasepool {
574 StorageManagerExample *example = [[StorageManagerExample alloc] init];
575
576 // Run all demonstrations
577 [example runAllDemonstrations];
578
579 // Cleanup
580 [example cleanup];
581
582 NSLog(@"Example completed successfully!");
583 }
584 return 0;
585}