FIRIAMServerMsgFetchStorage.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseCore/FIRLogger.h>
  17. #import "FIRCore+InAppMessaging.h"
  18. #import "FIRIAMServerMsgFetchStorage.h"
  19. @implementation FIRIAMServerMsgFetchStorage
  20. - (NSString *)determineCacheFilePath {
  21. NSString *cachePath =
  22. NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
  23. NSString *filePath = [NSString stringWithFormat:@"%@/firebase-iam-messages-cache", cachePath];
  24. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM150004",
  25. @"Persistent file path for fetch response data is %@", filePath);
  26. return filePath;
  27. }
  28. - (void)saveResponseDictionary:(NSDictionary *)response
  29. withCompletion:(void (^)(BOOL success))completion {
  30. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  31. if ([response writeToFile:[self determineCacheFilePath] atomically:YES]) {
  32. completion(YES);
  33. } else {
  34. completion(NO);
  35. }
  36. });
  37. }
  38. - (void)readResponseDictionary:(void (^)(NSDictionary *response, BOOL success))completion {
  39. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  40. NSString *storageFilePath = [self determineCacheFilePath];
  41. if ([[NSFileManager defaultManager] fileExistsAtPath:storageFilePath]) {
  42. NSDictionary *dictFromFile =
  43. [[NSMutableDictionary dictionaryWithContentsOfFile:[self determineCacheFilePath]] copy];
  44. if (dictFromFile) {
  45. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM150001",
  46. @"Loaded response from fetch storage successfully.");
  47. completion(dictFromFile, YES);
  48. } else {
  49. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM150002",
  50. @"Not able to read response from fetch storage.");
  51. completion(dictFromFile, NO);
  52. }
  53. } else {
  54. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM150003",
  55. @"Local fetch storage file not existent yet: first time launch of the app.");
  56. completion(nil, YES);
  57. }
  58. });
  59. }
  60. @end