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;
25- (void)initializeSdk {
29 self.sdk = [NavigineSdk getInstance];
34 [
self.sdk setUserHash:@"USER-HASH-HERE"];
39 [
self.sdk setServer:@"https://custom.navigine.com"];
44 self.storageManager = [
self.sdk getStorageManager];
47 if (
self.storageManager != nil) {
48 NSLog(
@"StorageManager successfully initialized");
50 }
@catch (NSException *exception) {
51 NSLog(
@"Error initializing SDK: %@", exception.reason);
58- (void)setupStorages {
59 if (
self.storageManager == nil) {
60 NSLog(
@"StorageManager not initialized");
66 self.userStorage = [
self.storageManager getStorage:@"user_preferences"];
67 self.appStorage = [
self.storageManager getStorage:@"app_settings"];
68 self.cacheStorage = [
self.storageManager getStorage:@"cache"];
71 NSLog(
@"Storage instances created");
77- (void)demonstrateStorageManagerMethods {
78 if (
self.storageManager == nil) {
79 NSLog(
@"StorageManager not initialized");
85 NSArray<NSString *> *storageList = [
self.storageManager getStorageList];
86 NSLog(
@"Existing storages: %@", storageList);
91 KeyValueStorage *testStorage = [
self.storageManager getStorage:@"test_storage"];
92 NSLog(
@"Created test storage");
96 [
self.storageManager getStorage:@"another_storage"];
97 NSArray<NSString *> *updatedList = [
self.storageManager getStorageList];
98 NSLog(
@"Updated storage list: %@", updatedList);
102 [
self.storageManager removeStorage:@"test_storage"];
103 NSLog(
@"Removed test storage");
106 NSArray<NSString *> *finalList = [
self.storageManager getStorageList];
107 NSLog(
@"Final storage list: %@", finalList);
113- (void)demonstrateKeyValueStorageMethods {
114 if (
self.userStorage == nil) {
115 NSLog(
@"User storage not initialized");
119 NSLog(
@"\n=== KeyValueStorage Methods Demonstration ===");
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");
130 [
self.userStorage putInt:@"user_age" value:25];
131 [
self.userStorage putInt:@"login_count" value:42];
132 NSLog(
@"Stored integer 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");
144 [
self.userStorage putBool:@"is_premium" value:YES];
145 [
self.userStorage putBool:@"notifications_enabled" value:NO];
146 NSLog(
@"Stored boolean values");
151 [
self.userStorage putFloat:@"user_rating" value:4.5f];
152 [
self.userStorage putFloat:@"temperature" value:23.5f];
153 NSLog(
@"Stored float 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");
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");
173 NSArray<NSString *> *allKeys = [
self.userStorage getKeys];
174 NSLog(
@"All stored keys: %@", allKeys);
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);
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);
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);
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");
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);
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);
239 [
self.userStorage remove:@"user_age"];
240 [
self.userStorage remove:@"non_existent_key"];
241 NSLog(
@"Removed 'user_age' key");
245 BOOL stillHasAge = [
self.userStorage contains:@"user_age"];
246 NSLog(
@"Still contains 'user_age': %@", stillHasAge ?
@"YES" :
@"NO");
250 [
self.userStorage clear];
251 NSLog(
@"Cleared all data from user storage");
255 NSArray<NSString *> *keysAfterClear = [
self.userStorage getKeys];
256 NSLog(
@"Keys after clear: %@", keysAfterClear);
262- (void)demonstrateMultipleStorages {
263 if (
self.userStorage == nil ||
self.appStorage == nil ||
self.cacheStorage == nil) {
264 NSLog(
@"Storages not initialized");
268 NSLog(
@"\n=== Multiple Storages Demonstration ===");
271 [
self.userStorage putString:@"name" value:@"John Doe"];
272 [
self.userStorage putInt:@"age" value:25];
273 [
self.userStorage putBool:@"is_premium" value:YES];
275 [
self.appStorage putString:@"theme" value:@"dark"];
276 [
self.appStorage putInt:@"api_version" value:2];
277 [
self.appStorage putBool:@"debug_mode" value:NO];
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];
284 NSLog(
@"User storage data:");
285 [
self demonstrateStorageData:
self.userStorage];
287 NSLog(
@"\nApp storage data:");
288 [
self demonstrateStorageData:
self.appStorage];
290 NSLog(
@"\nCache storage data:");
291 [
self demonstrateStorageData:
self.cacheStorage];
297- (void)demonstrateStorageData:(KeyValueStorage *)storage {
298 NSArray<NSString *> *keys = [storage getKeys];
299 for (NSString *key in keys) {
300 if ([storage contains:key]) {
302 NSString *stringValue = [storage getString:key defaultValue:@""];
303 if (stringValue.length > 0) {
304 NSLog(
@" %@: %@ (string)", key, stringValue);
309 int32_t intValue = [storage getInt:key defaultValue:-999999];
310 if (intValue != -999999) {
311 NSLog(
@" %@: %d (int)", key, intValue);
316 int64_t longValue = [storage getLong:key defaultValue:-999999];
317 if (longValue != -999999) {
318 NSLog(
@" %@: %lld (long)", key, longValue);
323 BOOL boolValue = [storage getBool:key defaultValue:NO];
324 if ([storage contains:key]) {
325 NSLog(
@" %@: %@ (bool)", key, boolValue ?
@"YES" :
@"NO");
330 float floatValue = [storage getFloat:key defaultValue:-999999.0f];
331 if (floatValue != -999999.0f) {
332 NSLog(
@" %@: %f (float)", key, floatValue);
337 double doubleValue = [storage getDouble:key defaultValue:-999999.0];
338 if (doubleValue != -999999.0) {
339 NSLog(
@" %@: %f (double)", key, doubleValue);
348- (void)demonstrateStorageAnalytics {
349 if (
self.storageManager == nil) {
350 NSLog(
@"StorageManager not initialized");
354 NSLog(
@"\n=== Storage Analytics ===");
357 NSArray<NSString *> *allStorages = [
self.storageManager getStorageList];
358 NSLog(
@"Total storages: %lu", (
unsigned long)allStorages.count);
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);
373 for (NSString *key in keys) {
375 NSString *stringValue = [storage getString:key defaultValue:@""];
376 if (stringValue.length > 0) {
381 int32_t intValue = [storage getInt:key defaultValue:-999999];
382 if (intValue != -999999) {
387 int64_t longValue = [storage getLong:key defaultValue:-999999];
388 if (longValue != -999999) {
393 BOOL boolValue = [storage getBool:key defaultValue:NO];
394 if ([storage contains:key]) {
399 float floatValue = [storage getFloat:key defaultValue:-999999.0f];
400 if (floatValue != -999999.0f) {
405 double doubleValue = [storage getDouble:key defaultValue:-999999.0];
406 if (doubleValue != -999999.0) {
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);
423- (void)demonstrateDataMigration {
424 if (
self.storageManager == nil) {
425 NSLog(
@"StorageManager not initialized");
429 NSLog(
@"\n=== Data Migration Demonstration ===");
432 KeyValueStorage *oldStorage = [
self.storageManager getStorage:@"old_storage"];
433 KeyValueStorage *newStorage = [
self.storageManager getStorage:@"new_storage"];
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];
441 NSLog(
@"Old storage before migration:");
442 [
self demonstrateStorageData:oldStorage];
445 NSArray<NSString *> *keys = [oldStorage getKeys];
446 for (NSString *key in keys) {
448 NSString *stringValue = [oldStorage getString:key defaultValue:@""];
449 if (stringValue.length > 0) {
450 [newStorage putString:key value:stringValue];
455 int32_t intValue = [oldStorage getInt:key defaultValue:-999999];
456 if (intValue != -999999) {
457 [newStorage putInt:key value:intValue];
462 int64_t longValue = [oldStorage getLong:key defaultValue:-999999];
463 if (longValue != -999999) {
464 [newStorage putLong:key value:longValue];
469 BOOL boolValue = [oldStorage getBool:key defaultValue:NO];
470 if ([oldStorage contains:key]) {
471 [newStorage putBool:key value:boolValue];
476 float floatValue = [oldStorage getFloat:key defaultValue:-999999.0f];
477 if (floatValue != -999999.0f) {
478 [newStorage putFloat:key value:floatValue];
483 double doubleValue = [oldStorage getDouble:key defaultValue:-999999.0];
484 if (doubleValue != -999999.0) {
485 [newStorage putDouble:key value:doubleValue];
489 NSLog(
@"\nNew storage after migration:");
490 [
self demonstrateStorageData:newStorage];
493 [
self.storageManager removeStorage:@"old_storage"];
494 NSLog(
@"\nRemoved old storage");
497 NSArray<NSString *> *finalStorages = [
self.storageManager getStorageList];
498 NSLog(
@"Final storage list: %@", finalStorages);
504- (void)demonstrateStorageCleanup {
505 if (
self.storageManager == nil) {
506 NSLog(
@"StorageManager not initialized");
510 NSLog(
@"\n=== Storage Cleanup Demonstration ===");
513 [
self.storageManager getStorage:@"temp_cache_1"];
514 [
self.storageManager getStorage:@"temp_cache_2"];
515 [
self.storageManager getStorage:@"permanent_storage"];
517 NSArray<NSString *> *beforeCleanup = [
self.storageManager getStorageList];
518 NSLog(
@"Storages before cleanup: %@", beforeCleanup);
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);
529 NSArray<NSString *> *afterCleanup = [
self.storageManager getStorageList];
530 NSLog(
@"Storages after cleanup: %@", afterCleanup);
536- (void)runAllDemonstrations {
537 NSLog(
@"=== StorageManager and KeyValueStorage Examples ===\n");
539 [
self demonstrateStorageManagerMethods];
540 [
self demonstrateKeyValueStorageMethods];
541 [
self demonstrateMultipleStorages];
542 [
self demonstrateStorageAnalytics];
543 [
self demonstrateDataMigration];
544 [
self demonstrateStorageCleanup];
546 NSLog(
@"\n=== All demonstrations completed ===");
553 if (
self.storageManager != nil) {
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];
572int main(
int argc,
const char * argv[]) {
577 [example runAllDemonstrations];
582 NSLog(
@"Example completed successfully!");