从实体获取所有数据并仅打印显示最后一条记录

时间:2012-03-21 13:45:38

标签: objective-c core-data nsfetchrequest

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

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];

2 个答案:

答案 0 :(得分:1)

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

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

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

您的下一个问题是此代码效率低下。您循环遍历同一个集合两次,一次循环记录,一次创建格式化字符串。您可以将它组合成一个循环。此外,所有变量都是在它们使用的for循环范围之外定义的,你可以简单地在每一行上声明每个变量。

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

答案 1 :(得分:0)

如果您尝试将所有formattedData个字符串连接到一个NSString printedData,那不是您的代码正在做什么,对吧?在对托管对象的每次迭代中,您将重新分配printedData。这就是为什么你只获得最后一条记录。

我推断您希望打印数据累积从formattedData个实例派生的所有NSManagedObject字符串。如果是这种情况,那么您需要将printedData声明为NSMutableString,并在每次迭代中将formattedData字符串附加到该可变字符串上,例如。

for( NSManagedObject *info in fetchedObjects )
{
    formattedData = [NSString stringWithFormat://blah blah blah
    [printedData appendString:formattedData];
}