matthew
matthew

Reputation: 477

Saving and receiving URL not working

I am trying to save a URL for a video to core data. To do this I am creating a string containing the URL, saving it to core data, and then retrieving it in another view controller. When retrieving it in the other View Controller, I get this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter' I have worked out that the URL being saved contains the URL, but when receiving it, it is nill. How would I fix this.

Here is the code to save the URL:

- (void) saveVideo {

    NSManagedObjectContext *context = [self managedObjectContext];

    TimeTravelDetails *timeTravelDetails = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTravelDetails" inManagedObjectContext:context];

    NSString *stringForSave = [self.videoURL absoluteString];
    NSLog(@"String being saved: %@", stringForSave);

    [timeTravelDetails setValue: stringForSave forKey:@"urlString"];

    NSError *error = nil;

    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } else {
        NSLog(@"URL String saved");
        NSLog(@"String being saved: %@", stringForSave);
    }

    //NSLog(@"%@", self.videoURL);

}

Here is were I retrieve it:

- (void) getUrl {

    NSManagedObjectContext *context = [self managedObjectContext];

    TimeTravelDetails *timeTravelDetails = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTravelDetails" inManagedObjectContext:context];

    NSString *stringForURL = [[NSString alloc] init];
    stringForURL = timeTravelDetails.urlString;
    NSLog(@"The string has been recieved: %@", stringForURL);
    self.finalURL = [[NSURL alloc] initWithString:stringForURL];

}

Upvotes: 0

Views: 59

Answers (1)

seriakillaz
seriakillaz

Reputation: 843

In the getUrl function you are creating a new core data object again (inserting a new obj to the database) and not querying the already saved data. sg like the following should work for querying the db:

 NSManagedObjectContext *moc = [self managedObjectContext];
 NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"TimeTravelDetails" inManagedObjectContext:moc];
 NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];

 NSError* error;
 NSArray *saved_time_travel_details = [moc executeFetchRequest:request error:&error];

Upvotes: 1

Related Questions