Search code examples
objective-cios8compareafnetworking-2xcode6.3

Compare API value to a local stored value at each request to API


OK, total brain freeze. I have a response from the CI Jenkins API. The result contains a successful build number for my project at Jenkins which increments each time a successful build on the Jenkins platform runs.

Now I want my app to check with a request if there is a new successful build, and if there is I want inform the user about it. So how can I store the current build number in the app and then compare it to each time I make a request to check the Jenkins build number to compare if the local stored build number is less than the build number on my Jenkins

[manager GET:@"http://jenkins.myProject/lastSuccessfulBuild/api/json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *responseDict = responseObject;

    NSInteger repsonsBuildNumber = [responseDict[@"number"] integerValue];

    if ([responseDict[@"result"] integerValue] == BuildStateSuccess && repsonsBuildNumber > self.currentBuildNumber ) {
        NSLog(@"There is a new succesful build at Jenkins");
    } else {
        NSLog(@"There currentBuildNumber is the same as the repsonsBuildNumber");
    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

So how can I store the currentBuildNumber to compare it to the repsonsBuildNumber whit each request?


Solution

  • NSUserDefaults is perfect for this. Just use -(NSInteger)integerForKey: to get the current build number stored:

     NSInteger currentBuildNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentBuildNumber"]];
     NSInteger repsonsBuildNumber = [responseDict[@"number"] integerValue];
    
     if (currentBuildNumber < repsonsBuildNumber) {
         // there is a new build
     }
    

    Then to set it, just use

    [[NSUserDefaults standardUserDefaults] setInteger:repsonsBuildNumber forKey:@"currentBuildNumber"]];