views:

22

answers:

0

Hi there, I have this code which checks whether a file exists, and if it does it queries the sqlite database to make sure there is an entry. It is used in the method cellForRowAtIndexPath in a UITableViewController.

Basically for each video that is listed in a Table I need to find if it EXISTS AND IS IN THE DATABASE.

Will this cost a lot of memory? ie: will it be slow/sluggish. The problem is that users can download a video and then the tableview has to reload, so I can't take the results out and (eg:) put it in an array on "init" because then when the table view won't update when I call reloadData.

Just want to make sure I'm doing the right thing.

- (BOOL)fileExists:(NSString *)filePath {
    if([[NSFileManager defaultManager] fileExistsAtPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:filePath]]){
        // the file exists...
        NSError *theError;

        // new request 
        NSFetchRequest *req = [NSFetchRequest new];
        // add the description...
        NSEntityDescription *descr = [NSEntityDescription entityForName:@"Videos" inManagedObjectContext:context];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(localFilename = %@)", filePath];
        [req setEntity:descr];
        [req setPredicate:predicate];

        NSArray *resultData = [context executeFetchRequest:req error:&theError];
        //NSError *error;

        if([resultData count] > 0) {
            return true;
        }
    }
    return false;
}