welcome to linkAR technical documentation

next previous

Downloading TMDB Data:

In MainViewcontroller.m we have:

-(void)initMoviesHandler{
    ...    
    if (myDate == nil || [currentDate timeIntervalSinceDate:myDate]/360 >= 24) {
        [self performSelectorInBackground:@selector(downloadPosters) withObject:nil];
    }else {
        [moviesHandler performSelectorInBackground:@selector(reloadMoviesFromDB) withObject:nil];
    }
}

If the application has not saved the date since 24 hours ago, it downloads the movies. Otherwise it reloads the movies from the NSDocumentsDirectory.
In moviesDataHandler.m we have:

  • Download movies:
-(BOOL)updateMovies
{
    ...
    //1
        NSString* urlString = [NSString stringWithFormat:@"%@api_key=%@&page=%d",tmdbURL,tmdbApiKey,i];
        NSURL* url = [[NSURL alloc] initWithString:urlString];
        NSData* dat = [[NSData alloc] initWithContentsOfURL:url];
     ...
 
    //2.- Send a notification to initialize the UIProgressView
    [[NSNotificationCenter defaultCenter] postNotificationName:@"InitializeProgress" object:[[NSNumber alloc] initWithInt:[moviesList count]]];
 
    //3
    for (NSDictionary* movieDict in moviesList) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MovieAdded" object:movieDict];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"IncrementProgress" object:movieDict];
        ...
    }
    //4
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemoveAlert" object:nil];
    ...
}

1- Downloads TMDB api data.
2- When the download starts, the UIProgressView is initialized.
3- For each movie, adds the image poster into the library and increments the UIProgressView. If the image has been added, writes the movie into the NSDocumentsDirectory and adds it into an array.
4- When the download finishes, removes the alert with the UIProgressView from superview.

  • Reload movies from NSDocumentDirectory:
-(void)reloadMoviesFromDB
{    
    //1
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];     
    ...
 
    NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];
 
    for (NSString *path in directoryEnumerator) 
    {    
        ...
 
        movie.movieId = fileName.intValue;
        movie.movieInfo = [NSDictionary dictionaryWithContentsOfFile:dictPath];
        movie.posterImage = [UIImage imageWithContentsOfFile:imagePath];
 
        //2
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MovieAdded" object:movie];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"IncrementProgress" object:movie];
        }
    }
    //3
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemoveAlert" object:nil];
}

1-Load the movie from the NSDocumentsDirectory.
2-For each movie, adds the image poster into the library and increments the UIProgressView. If the image has been added, adds it into the array.
3-When the download finishes, removes the alert with the UIProgressView from superview.

next previous