rosshump
rosshump

Reputation: 370

Retrieve last row/object in Core Data entity as a string

I need to develop a simple login system for my Questionnaire app.

I have two Entity's - one for saving Questionnaire data called NGLS which stores client details, survey answers, etc, and another called Admin for the user. The user can type in their username which gets saved to the Admin entity. They can then run through the Questionnaire as many times as they like, and each completed survey gets saved to the NGLS entity after every survey like so:

    // Save managedObject
    NSError *error;
    [[self.managedObjectNGLS managedObjectContext] save:&error];
    NSLog(@"%@", self.managedObjectNGLS);
    NSLog(@"Data saved"); 

There is an export button which uses a NSFetchRequest to retrieve all data stored in the NGLS entity, and uses the CHCSV Parser to write that data to a .csv which is then attached to an email.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"NGLS"
                                              inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    // CHCSVParser
    NSOutputStream *stream = [[NSOutputStream alloc]initToMemory];
    CHCSVWriter *writer = [[CHCSVWriter alloc]initWithOutputStream:stream
                                                          encoding:NSUTF8StringEncoding
                                                         delimiter:','];

    // Fetch objects to write to .csv
    for (Model *results in fetchedObjects) {
        [writer writeLineOfFields:@[results.username,
                                    results.dateStamp,
                                    ...,

What I need to do is retrieve the last stored username in the Admin entity and save it to the username key in my NGLS entity upon each completed survey. This will essentially allow a user to open the app and "log in" once, complete the questionnaire multiple times, and each row in the .csv will have that username - eliminating the need for them to type in a username upon every completed survey.

I have tried to fetch the lastObject in the Admin entity by using this code:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Admin" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

NSLog(@"Last username is %@", [fetchedObjects lastObject]);

But the output is:

Last username is <NSManagedObject: 0xb7a20d0> (entity: Admin; id: 0xb7a1410 <x-coredata://1D073AF9-3FFB-476A-A6CC-BC15398F4B86/Admin/p2> ; data: <fault>)

Can anyone help me achieve this? Any help is greatly appreciated. If you can think of an easier way to "log in", I'll gladly take suggestions. Thanks.

Upvotes: 0

Views: 679

Answers (1)

Mrunal
Mrunal

Reputation: 14118

Try this:

Admin *model = [fetchedObjects lastObject]; NSLog(@"Last username is %@", model.username);

Upvotes: 1

Related Questions