以字符串形式检索Core Data实体中的最后一行/对象 [英] Retrieve last row/object in Core Data entity as a string
问题描述
我需要为我的问卷应用程序开发一个简单的登录
系统。
我有两个实体
- 一个用于保存问卷调查数据 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 calledNGLS
which stores client details, survey answers, etc, and another calledAdmin
for the user. The user can type in theirusername
which gets saved to theAdmin
entity. They can then run through the Questionnaire as many times as they like, and each completed survey gets saved to theNGLS
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 aNSFetchRequest
to retrieve all data stored in theNGLS
entity, and uses theCHCSV 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 theAdmin
entity and save it to theusername
key in myNGLS
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 thatusername
- eliminating the need for them to type in ausername
upon every completed survey.I have tried to fetch the
lastObject
in theAdmin
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屋!