-1

I have downloaded a plist file from server, that contains key value pair. Once app resumes/restarts then again I have to download file and check if file has changed.

below is the code to download... I am storing the key values in NSDictionary.

 task1 = [session dataTaskWithURL:[NSURL URLWithString:[S3_SERVER_URL stringByAppendingString:propFile]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    propfilePath = [documentsDirectory stringByAppendingString:propFile];
    NSLog(@"DestPath : %@", propfilePath);
    [receivedData appendData:data];
    NSLog(@"Succeeded! Received %lu bytes of data",(unsigned long)[data length]);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [data writeToFile:propfilePath atomically:YES];

    if(error == nil){
        plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:propfilePath] retain];
        [task2 resume];
    } else {

    }

}];

How can i compare the contents of the two plist files or NS dictionary? Which function is best suited to do the above As I have to do this on app create/resume/restart? It should be compatible to both ios7 and ios8 SDK.

Anjali
  • 1,623
  • 5
  • 30
  • 50

2 Answers2

3

If you want all changed key follow this:- This will return array of all changed keys.

Create a category for NSDictionary

NSDictionary+newDict.h

#import <Foundation/Foundation.h>

@interface NSDictionary (newDict)
- (NSArray*)changedKeysIn:(NSDictionary*)d;
@end

NSDictionary+newDict.m

#import "NSDictionary+newDict.h"

@implementation NSDictionary (newDict)

- (NSArray*)changedKeysIn:(NSDictionary*)d {
    NSMutableArray *changedKs = [NSMutableArray array];
    for(id k in self) {
        if(![[self objectForKey:k] isEqual:[d objectForKey:k]])
            [changedKs addObject:k];
    }
    return changedKs;
}
@end

Calling:-

#import "NSDictionary+newDict.h"

and:-

NSArray *keys = [dict1 changedKeysIn:dict2];
NSLog(@"%@", keys);   
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65