-2

I have a web service to fetch the data in the JSON form. I am calling this service everytime the application is launching (inside appDidFinishLaunching method) and storing it in a local file. I want to call the service only for the first time app is launching and from the next time i just wanted to use the file in which i have stored my JSON Data.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //live json data url

    NSURL *url = [NSURL URLWithString:@"My Url"];
    NSData *urlData = [NSData dataWithContentsOfURL:url];

    //attempt to download live data
    if (urlData)
    {
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
        [urlData writeToFile:filePath atomically:YES];

    }
    //copy data from initial package into the applications Documents folder
    else
    {
        //file to write to
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];

        //file to copy from
        NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ];
        NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];

        //write file to device
        [jsonData writeToFile:filePath atomically:YES];
    }

    NSError *jsonError = nil;

    NSString *jsonFilePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:jsonFilePath options:kNilOptions error:&jsonError ];
    NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
    self.serverResponseArray = jsonArray;
    NSLog(@"%@",self.serverResponseArray);

By this code I am writing JSON response to a local file and storing in an array to use it in the whole app.

Zac24
  • 2,514
  • 1
  • 23
  • 28
  • 1
    You could just test the presence of the file before trying to load the data no? – talnicolas May 08 '13 at 20:58
  • How do i check that. ? – Zac24 May 08 '13 at 20:58
  • The first time you gonna load the json, and store it in a local file with a specific name (if I understand well). The next time you launch the app, if the file with that name exists you don't need to load the json, just use the file. But just out of curiosity don't you want that data to be updated every time you launch the app? – talnicolas May 08 '13 at 21:01
  • your curiosity is actually fine but i need that data even if suddenly internet goes off or the basement of building have poor internet. well still could you tell how do we check that if that file name is there. i have no clue how to check the presence of file.. any link would be helpful. – Zac24 May 08 '13 at 21:05
  • There are a lot of resources talking about it on google, it's fairly easy, just search objective-c write/read file. – talnicolas May 08 '13 at 21:09

2 Answers2

2
   static NSString * const someKey = @"haveChecked";
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    NSUserDefaults * stdUserDefaults = [NSUserDefaults standardUserDefaults];
    if(![[stdUserDefaults objectForKey:someKey] boolValue])
    {
        [self trySomething];
    }
}
-(void)gotJson
{
    //async callback
    NSUserDefaults * stdUserDefaults = [NSUserDefaults standardUserDefaults];
    [stdUserDefaults setBool:YES forKey:someKey];
    [stdUserDefaults synchronize];

}

it sounds like you need to learn about dealing with files, so here is some stack overflow reading:

What is the documents directory (NSDocumentDirectory)?
Write a file on iOS

Community
  • 1
  • 1
Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • 1
    I don't think that will solve the original question, which was to load the JSON on the first launch only. Those all ensure something happens at every launch, once. – Jesse Rusak May 08 '13 at 21:14
0

try this

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

            if ([self fetchFromNSUserDefaults:@"FirstTimeNO"]==nil || [FunctionManager fetchFromNSUserDefaults:@"FirstTimeNO"]==@"") {
                 [self YourFunctionThatCallFirstTime];
            }
        return YES;
    }

    -(id)fetchFromNSUserDefaults:(NSString *)strKey{
             NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults];
              return [objUserDefaults objectForKey:strKey];
    }

    -(void)addToNSUserDefaults:(id)objValue forKey:(NSString *)strKey{
        NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults];
        [objUserDefaults setObject:objValue forKey:strKey];
        [objUserDefaults synchronize];
    }

    -(void)YourFunctionThatCallFirstTime {
                 //Your Stuff

           [self addToNSUserDefaults:@"1" forKey:@"FirstTimeNO"];
     }
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45