簡體   English   中英

為iPhone日歷應用程序存儲數據的最佳方法

[英]Best way to store data for an iPhone calendaring app

我正在使用iPhone應用程序。 這是我的代理機構的企業日歷應用程序。 我們有一個RESTful API后端連接到輸入約會的Web應用程序。 個人的約會將顯示在該用戶的iPhone上。 數據通過XML發送。 每天只有3或4個約會,每條記錄少於10個字段,因此一次不會傳輸大量數據(只是所選日期的信息)。

我嘗試使用iPhone上的數組設計用於解析的數據,但是Web服務器上的安全性檢查使應用程序在加載數據時超時,因此我無法很好地處理異步處理。

現在我想知道我是否正在正確地解決問題。 使用Core Data存儲約會,然后在后台更新Core Data存儲會更好嗎? 我知道我需要在加載表過程之外更新數據。 我不知所措地尋求最佳解決方案。

我瀏覽了該網站,以獲取有關如何進行此操作的信息。 我試圖看書。 任何幫助,將不勝感激。

安全性

typedef void (^touchIDComplete)(BOOL);
typedef void (^fileExists)(BOOL);
typedef void (^sessionVerify)(BOOL);
typedef void (^parsingData)(BOOL);
typedef void (^touchIDSuccess)(BOOL);
typedef void (^sessionRetrieved)(BOOL);
typedef void (^touchIDComplete)(BOOL);
typedef void (^sessionReading)(BOOL);
typedef void (^fillArray)(BOOL);
typedef void (^getTheData)(NSData *myData, NSError *error);
typedef void (^gettingESNBlock)(NSString *myESN, NSString *newSession, BOOL success, NSError *error);
typedef void (^checkingESNBlock)(NSString *myESN, NSString *sessionInfo, BOOL success, NSError *error);


@interface Security : NSObject

@property (strong, nonatomic) NSArray *types;
@property (strong, nonatomic) NSArray *esn;
@property (strong, nonatomic) NSString *idfv;
@property (strong, nonatomic) NSData *parseData;
@property (strong, nonatomic) NSString *sessionDetail;
@property (strong, nonatomic) NSString *loginFinished;
@property (strong, nonatomic) NSMutableURLRequest *request;
@property (atomic) NSString *passESN;

- (void)waitForData:(sessionVerify)compblock;
- (void)waitForFile:(fileExists)compblock;
- (void)waitForESN:(parsingData)compblock;
- (void)findESN:(gettingESNBlock)callback;
- (void)checkThumb:(touchIDSuccess)compblock;
- (void)readIt:(sessionRetrieved)compblock;
- (void)readNewSession:(sessionReading)compblock;
- (void)doTheWork:(NSString *)theESN withSession:(NSString *)newSession withSuccess:(BOOL)success error:(NSError *)error;
- (void)checkESN:(checkingESNBlock)callback;
- (void)checkTheSession:(NSString *)oldESN withSession:(NSString *)oldSession withSuccess:(BOOL)success error:(NSError *)error;
- (void)fillAppointmentData:(fillArray)compblock;
- (void)gettingData:(getTheData)compblock;

@end

安全性

@implementation Security

void(^getESNForCallback)(NSString *myESN, NSString *newSession, BOOL success, NSError *error);
void(^checkESNWithCallback)(NSString *myESN, NSString *oldSession, BOOL success, NSError *error);

- (void)waitForFile:(fileExists) compblock {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
    NSString *fullPath = [documentsDirectoryPath stringByAppendingString:@"/session.txt"];
    compblock([fileManager fileExistsAtPath:fullPath]);
}

- (void) waitForData:(sessionVerify) compblock {
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:self.request
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                            if (error) {
                                                return;
                                            }

                                            if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                                NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

                                                if (statusCode != 200) {
                                                    if (statusCode == 401) {
                                                        // Insert process for thumbprint and session cookie pull
                                                        NSFileManager *fileManagerThree = [NSFileManager defaultManager];
                                                        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                                                        NSString *sessionPath = [documentsPath stringByAppendingPathComponent:@"session.txt"];
                                                        NSError *error;
                                                        BOOL success = [fileManagerThree removeItemAtPath:sessionPath error:&error];
                                                        if (success) {
                                                        } else {
                                                        }
                                                    } else {
                                                        return;
                                                    }
                                                }
                                            }
                                            self.parseData = data;
                                            compblock (YES);
                                        }];
    [task resume];
}

- (void)waitForESN:(parsingData) compblock {
    ParseTypeXML *myParser = [[ParseTypeXML alloc] initWithData:self.parseData];
    VariableStore *globals = [VariableStore sharedInstance];
    if ([myParser.esn count] == 0) {
        globals.user_esn = @"Error";
        compblock(YES);
    } else {
        globals.user_esn = myParser.esn[0];
        compblock(YES);
    }
}

- (void)findESN:(gettingESNBlock)callback {
    getESNForCallback = callback;
    VariableStore *globals = [VariableStore sharedInstance];
    [self doTheWork:globals.user_esn withSession:globals.sessionInfo withSuccess:YES error:nil];
}

- (void)doTheWork:(NSString *)theESN withSession:(NSString *)newSession withSuccess:(BOOL)success error:(NSError *)error {
    [self checkThumb:^(BOOL finished) {
        if(finished) {
            [self readIt:^(BOOL newFile) {
                if (newFile) {
                    [self readNewSession:^(BOOL seen) {
                        if (seen) {
                            VariableStore *globals = [VariableStore sharedInstance];
                            NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                                        @"ollie/", NSHTTPCookieDomain,
                                                        @"\\", NSHTTPCookiePath,
                                                        @"Cookie", NSHTTPCookieName,
                                                        globals.sessionInfo, NSHTTPCookieValue,
                                                        nil];
                            NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
                            NSArray *cookieArray = [NSArray arrayWithObject:cookie];
                            NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];

                            NSMutableString *url = [[NSMutableString alloc] initWithString:@"https://company.com/file.php"];

                            NSURL *urlNew = [NSURL URLWithString:url];
                            self.request = [NSMutableURLRequest requestWithURL:urlNew];
                            [self.request setHTTPMethod:@"GET"];
                            [self.request setAllHTTPHeaderFields:headers];

                            [self waitForData:^(BOOL dataReceived) {
                                if (dataReceived) {
                                    [self waitForESN:^(BOOL esnFound) {
                                        if (esnFound) {
                                            VariableStore *globals = [VariableStore sharedInstance];
                                            getESNForCallback(globals.user_esn, globals.sessionInfo, success, error);
                                        }
                                    }];
                                }
                            }];
                        }
                    }];
                }
            }];
        }
    }];
}

- (void)checkThumb:(touchIDSuccess)compblock {
    LAContext *context = [[LAContext alloc] init];
    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        // Authenticate User
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
            localizedReason:@"You need to log in."
                      reply:^(BOOL success, NSError * _Nullable error) {
                          if (success) {
                              NSLog(@"success");
                              compblock(YES);
                          } else {
                              switch (error.code) {
                                  case LAErrorAuthenticationFailed:
                                      break;
                                  case LAErrorUserCancel:
                                      break;
                                  case LAErrorUserFallback:
                                      break;

                                  default:
                                      break;
                              }
                          }
                      }];
    }
}

- (void)readIt:(sessionRetrieved)compblock {
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSString *url = @"https://company.com/specialstring.php";

    NSMutableString *postText = [[NSMutableString alloc] init];

    [postText appendString:idfv];

    NSString *postBody = [NSString stringWithString:postText];

    XMLPostSecurity *postAction = [[XMLPostSecurity alloc] init];
    VariableStore *globals = [VariableStore sharedInstance];
    globals.sessionInfo = [postAction sendPostRequestToUrl:url withBody:postBody];

    FileSaving *saver = [[FileSaving alloc] init];

    [saver saveSession:globals.sessionInfo];
    compblock(YES);
}

-(void)readNewSession:(sessionReading)compblock {
    NSFileManager *fileManagerTwo;
    NSData *dataBuffer;
    fileManagerTwo = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingString:@"/session.txt"];

    dataBuffer = [fileManagerTwo contentsAtPath:filePath];
    VariableStore *globals = [VariableStore sharedInstance];
    globals.sessionInfo = [[NSString alloc] initWithData:dataBuffer encoding:(NSASCIIStringEncoding)];
    compblock(YES);
}

- (void)checkESN:(checkingESNBlock)callback {
    checkESNWithCallback = callback;
    VariableStore *globals = [VariableStore sharedInstance];
    [self checkTheSession:globals.user_esn withSession:globals.sessionInfo withSuccess:YES error:nil];
}

- (void)checkTheSession:(NSString *)theESN withSession:(NSString *)oldSession withSuccess:(BOOL)success error:(NSError *)error {
    [self readNewSession:^(BOOL seen) {
        if (seen) {
            VariableStore *globals = [VariableStore sharedInstance];
            NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"ollie/", NSHTTPCookieDomain,
                                            @"\\", NSHTTPCookiePath,
                                            @"Cookie", NSHTTPCookieName,
                                            globals.sessionInfo, NSHTTPCookieValue,
                                            nil];
            NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
            NSArray *cookieArray = [NSArray arrayWithObject:cookie];
            NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];

            NSMutableString *url = [[NSMutableString alloc] initWithString:@"https://company.com/file.php"];

            NSURL *urlNew = [NSURL URLWithString:url];
            self.request = [NSMutableURLRequest requestWithURL:urlNew];
            [self.request setHTTPMethod:@"GET"];
            [self.request setAllHTTPHeaderFields:headers];

            [self waitForData:^(BOOL dataReceived) {
                if (dataReceived) {
                    [self waitForESN:^(BOOL esnFound) {
                        if (esnFound) {
                            VariableStore *globals = [VariableStore sharedInstance];
                            checkESNWithCallback(globals.user_esn, globals.sessionInfo, success, error);
                        }
                    }];
                }
            }];
        }
    }];
}

- (void)fillAppointmentData:(fillArray)compblock {

    VariableStore *globals = [VariableStore sharedInstance];

    NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"ollie/", NSHTTPCookieDomain,
                                  @"\\", NSHTTPCookiePath,
                                  @"Cookie", NSHTTPCookieName,
                                  globals.sessionInfo, NSHTTPCookieValue,
                                  nil];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
    NSArray *cookieArray = [NSArray arrayWithObject:cookie];
    NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];

    NSMutableString *url = [[NSMutableString alloc] initWithString:@"https://company.com/file2.php?adb="];

    [url appendString:globals.chosenDate];
    [url appendString:@"&esn="];
    [url appendString:globals.user_esn];

    NSURL *urlNew = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlNew];
    [request setHTTPMethod:@"GET"];
    [request setAllHTTPHeaderFields:headers];

    [self gettingData:^(NSData *myData, NSError *error) {
        if (myData != nil) {
            ParseXML *myParser = [[ParseXML alloc] initWithData:myData];

            [globals.appointmentData removeAllObjects];

            [globals.appointmentData addObjectsFromArray:myParser.items];
        }
    }];
}

- (void) gettingData:(getTheData) compblock {
    VariableStore *globals = [VariableStore sharedInstance];
    globals.got401 = nil;
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:self.request
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                            if (error) {
                                                return;
                                            }

                                            if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                                NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

                                                if (statusCode != 200) {
                                                    if (statusCode == 401) {
                                                        // Insert process for thumbprint and session cookie pull
                                                        NSFileManager *fileManagerThree = [NSFileManager defaultManager];
                                                        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                                                        NSString *sessionPath = [documentsPath stringByAppendingPathComponent:@"session.txt"];
                                                        NSError *error;
                                                        BOOL success = [fileManagerThree removeItemAtPath:sessionPath error:&error];
                                                        if (success) {
                                                        } else {
                                                        }
                                                        globals.got401 = @"Error";
                                                    } else {
                                                        return;
                                                    }
                                                }
                                            }
                                            self.parseData = data;
                                        }];
    [task resume];
}

@end

如果本地僅存儲3或4個約會數據,那么答案是“最適合您的條件”。 真的沒關系。 您可以將數據轉換為莫爾斯電碼並保存點和破折號,然后讀取該數據,它仍然足夠小且足夠快。

您可以將數據保存到plist,使用NSCoding進行序列化,將其保存為SQLite數據庫,甚至可以編寫XML並在讀取數據時將其轉換回數組(盡管XML選項可能是最慢/效率最低的)。

核心數據非常強大(非常酷),但是學習曲線也非常陡峭。 在您習慣使用iOS之前,我不建議您這樣做。

如果您的應用程序超時,則可能還有其他問題。 編輯您的問題以顯示問題區域的代碼,也許我們可以為您提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM