以字符串形式检索Core Data实体中的最后一行/对象 [英] Retrieve last row/object in Core Data entity as a string

查看:180
本文介绍了以字符串形式检索Core Data实体中的最后一行/对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的问卷应用程序开发一个简单的登录系统。



我有两个实体 - 一个用于保存问卷调查数据 NGLS 其中存储客户端详细信息,调查答案等,另一个名为 Admin 的用户。用户可以键入其用户名,该用户名将保存到 Admin 实体。然后,他们可以按照自己喜欢的次数运行问卷调查,每次调查后,每个完成的调查都会保存到 NGLS 实体中:

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

有一个 export 按钮, NSFetchRequest 以检索存储在 NGLS 实体中的所有数据,并使用 CHCSV Parser 将该数据写入随后附加到电子邮件的.csv。

  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:','];

//获取对象写入.csv
for(Model * results in fetchedObjects){
[writer writeLineOfFields:@ [results.username,
results。 dateStamp,
...,

我需要做的是检索最后存储的<实体中的code> username ,并将其保存到用户名键在每个完成的调查我的 NGLS 实体。这将基本上允许用户打开应用程序和登录一次,完成问卷多次,.csv中的每一行将有 username - 消除需要他们在每次完成的调查中键入用户名



我试图获取<$ c使用以下代码在 Admin 实体中创建$ c> lastObject

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

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

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

但输出是:

 上次用户名是< NSManagedObject:0xb7a20d0> (entity:Admin; id:0xb7a1410< x-coredata:// 1D073AF9-3FFB-476A-A6CC-BC15398F4B86 / Admin / p2>; data:< fault>)
/ pre>

任何人都可以帮助我实现这一点?任何帮助是非常感激。如果你能想到一个更简单的方法登录,我会很乐意采纳建议。感谢。

解决方案

尝试:

  Admin * model = [fetchedObjects lastObject]; NSLog(@上次用户名是%@,model.username); 


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.

解决方案

Try this:

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

这篇关于以字符串形式检索Core Data实体中的最后一行/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆