从实体获取所有数据并仅打印显示最后一条记录 [英] Fetching all data from an Entity and printing only displays the last record

查看:98
本文介绍了从实体获取所有数据并仅打印显示最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从特定的Core Data Entity中获取所有的数据,并将每个记录放在一个字符串中。但是下面的代码只打印它访问的LAST记录。我做错了什么?

I'm trying to fetch all of the data from a particular Core Data Entity and place each record into a string. However the following code only prints the LAST record that it accesses. What am I doing wrong?

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order" inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];


NSLog(@"=============================================");
NSLog(@"Generating Data To Be Sent");
for (NSManagedObjectContext * info in fetchedObjects) 

{

    NSLog(@"Client Name: %@", [info valueForKey:@"clientName"]);
    NSLog(@"Client Account Number: %@", [info valueForKey:@"clientAccountNumber"]); 
    NSLog(@"Product Code: %@", [info valueForKey:@"productCode"]);
    NSLog(@"Product Price: %@", [info valueForKey:@"productPrice"]);
    NSLog(@"Product Quantity: %@", [info valueForKey:@"productQuantity"]);
    NSLog(@"Order Total: %@", [info valueForKey:@"orderTotal"]);
    NSLog(@"Order ID : %@", [info valueForKey:@"orderID"]); 
    NSLog(@"Transaction ID: %@", [info valueForKey:@"transactionID"]);
    NSLog(@"Sales Rep ID : %@", [info valueForKey:@"salesRepresentativeID"]);

}

NSString * printedData;
NSString * formattedData;

NSString * clientName;
NSString * clientAccount;
NSString * productCode;
NSString * productQuantity;
NSString * productPrice;
NSString * orderTotal;
NSString * orderID;
NSString * transactionID;
NSString * salesRepID;



for(NSManagedObject * info in fetchedObjects)

{

    clientName = [info valueForKey:@"clientName"];
    clientAccount =  [info valueForKey:@"clientAccountNumber"]; 
    productCode =  [info valueForKey:@"productCode"];
    productPrice =  [info valueForKey:@"productPrice"];
    productQuantity = [info valueForKey:@"productQuantity"];
    orderTotal =  [info valueForKey:@"orderTotal"];
    orderID = [info valueForKey:@"orderID"]; 
    transactionID = [info valueForKey:@"transactionID"];
    salesRepID = [info valueForKey:@"salesRepresentativeID"];


    formattedData = [NSString stringWithFormat:@"|%@|%@|%@|%@|%@|%@|%@|%@|%@|\n",clientName,clientAccount,productCode,productQuantity,productPrice,orderID,transactionID,orderTotal,salesRepID];

    printedData = formattedData;

}

NSLog(@"=============================================");
NSLog(@"Data Generated");
NSLog(@"%@",printedData);
[fetchRequest release];

NSMutableArray *recipients = [[NSMutableArray alloc] initWithCapacity:1];
[recipients addObject:toEmail.text];

MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:toSubject.text];
[controller setMessageBody:printedData isHTML:NO];
[controller setToRecipients:recipients];

[self presentModalViewController:controller animated:YES];
[controller release];


推荐答案

要解决您的初始问题, code> NSMutableString 。不需要 formattedData 变量。

To fix your initial problem you need to use an NSMutableString. There is no need for formattedData variable.

...
NSMutableString * printedData = [NSMutableString string];
...

for(NSManagedObject * info in fetchedObjects)
{
    ...
    [printedData appendFormat:@"|%@|%@|%@|%@|%@|%@|%@|%@|%@|\n",clientName,clientAccount,productCode,productQuantity,productPrice,orderID,transactionID,orderTotal,salesRepID];
}
...

您的下一个问题是。您正在循环相同的集合两次,一次日志,一次创建格式化的字符串。你可以将它组合成一个循环。所有的变量都定义在它们所使用的for循环的范围之外,你可以简单地在一行上像这样声明每一个。

Your next issue is how inefficient this code is. You are looping over the same collection twice, once to log, once to create a formatted string. You can combine this into one loop. Also all of the variables are defined outside of the scope of the for loop that they are used in, you could simply just declare each one on a line like this.

for(NSManagedObject * info in fetchedObjects)
{
    NSString *clientName = [info valueForKey:@"clientName"];
    NSString *clientAccount =  [info valueForKey:@"clientAccountNumber"];
    ...
    //Log them all
    NSLog(@"%@",clientName);
    NSLog(@"%@",clientAccount);
    ...
}

这篇关于从实体获取所有数据并仅打印显示最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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