打印fetchrequest的内容

时间:2012-03-21 14:30:02

标签: objective-c ios core-data

  

可能重复:
  Fetching all data from an Entity and printing only displays the last record

早安的人,

我遇到了一个fetchrequest的问题,我已经设置了获取特定Core Data Entity中的所有数据并将每条记录放入一个字符串中,我的代码可以在下面找到:

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

}

现在问题是它只打印出它访问的最后一条记录,我知道它很愚蠢,但是我无法解释我如何解决这个问题

谢谢大家:D -Chris

0 个答案:

没有答案