iOS.Lover
iOS.Lover

Reputation: 6051

iOS : Call a method just one time

Hi I was wondering how can I call a method just for one time in application life ... My application should download some files from server and I need do it just for one time; I mean mean just one time per installation

here is my method

//Download some images from server and save it into directory 

- (void) downloadCovers {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    [self saveFile:@"mag1" ofType:@"png" fromURL:@"http://myweb.com/mag1.png" inDirectory:documentsDirectory];

}

and this method set images as UIButton BG :

  - (void)buttonsBGImage {

       UIImage * bgMag1 = [self loadImage:@"mag1" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

        [mag1 setBackgroundImage:bgMag1 forState:UIControlStateNormal];
         NSLog(@"BG IS SET");

    }

Upvotes: 1

Views: 5262

Answers (4)

Imrane
Imrane

Reputation: 196

why not just testing if the file is exist or not in local storage!

//Download some images from server and save it into directory 

- (void) downloadCovers {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pathToImg = [NSString stringWithFormat:@"%@/mag1.png",documentsDirectory];
    BOOL isExist = [[NSFileManager defaultManager]fileExistsAtPath:pathToImg];
    if (!isExist) {
        [self saveFile:@"mag1" ofType:@"png" fromURL:@"http://myweb.com/mag1.png" inDirectory:documentsDirectory];
    }

}

Upvotes: 6

Mario
Mario

Reputation: 4520

Set a flag as a NSUserDefaults key and check for this NSUserDefault value in your downloadCovers method. If it is already set, do nothing, else download files and set the flag to true.

Like so:

-(void) downloadCovers {
BOOL downloaded = [[NSUserDefaults standardUserDefaults] boolForKey: @"downloaded"];
if (!downloaded) {
     //download code here
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey: @"downloaded"];
    }
}

Cheers

Upvotes: 3

justin
justin

Reputation: 104698

You can't do it for a method, but you can do it for a function using pthread_once:

static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(& once, SomeFunction);

or you can execute a block once using dispatch_once (the most natural choice for your current implementation).

In some cases (not this one), you may also prefer to do your work in +initialize.

EDIT: Question was clarified

Just check for the file's existence, or use a preference if you want this to persist across multiple launches.

Upvotes: 4

meronix
meronix

Reputation: 6176

 - (void)buttonsBGImage {

        if (!mag1.backgroundImage){
             UIImage * bgMag1 = [self loadImage:@"mag1" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

               [mag1 setBackgroundImage:bgMag1 forState:UIControlStateNormal];
               NSLog(@"BG IS SET");
        }

    }

Upvotes: 0

Related Questions