Export Core Data Entity as text files in Cocoa

那年仲夏 提交于 2019-12-03 08:02:26

问题


I have an entity in core data that has 2 Attributes. One that is a string called "name", and another one that is a string called "message". I need a method to create text files for all the attributes that the user has added. I wan't the files names to be the name attribute and the contents to be the message attribute. If anyone knows how to do this any help would be great.

Thanks for any help


回答1:


Have you given any thought at all to the steps involved?

  1. Create a fetch request (possibly with a predicate if you want to filter the results).
  2. Execute the fetch request. If successful ...
  3. For each instance in the returned results array ...
  4. Create a path using the "name" attribute.
  5. Write the "message" string to the path you just created.
  6. Next instance...

Once you've identified the steps, it's not difficult at all to look up the relevant parts of the documentation to figure out how to do each step.

You could actually perform this in three lines of code if you inline some things and have already verified the target folder is writable and don't care about overwriting.

Update

Okay, so it takes a minimum of four lines since you can't init a fetch request with an entity description, assuming you ignore fetch errors. Since you asked for the code:

NSFetchRequest *fr = [[[NSFetchRequest alloc] init] autorelease];
[fr setEntity:[NSEntityDescription entityForName:@"EntityName" 
                          inManagedObjectContext:[self managedObjectContext]]] 
        error:nil];
NSArray *results = [[self managedObjectContext] executeFetchRequest:fr error:nil];
for (id result in results){
    [[result valueForKey:@"message"] writeToFile:[basePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", [result valueForKey:@"name"]]]
              atomically:NO]
};

Of course this isn't very readable (ie, it's ugly) and makes all sorts of assumptions, but you get the point. Here's a more complete (and illustratively verbose) way:

// Create the fetch request
NSFetchRequest * fr = [[[NSFetchRequest alloc] init] autorelease];
[fr setEntity:[NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:[self managedObjectContext]]];

// Execute the fetch request
NSError * error = nil;
NSArray * results = [[self managedObjectContext] executeFetchRequest:fr error:&error];
if (results)
{
    // Process the results
    NSString * name;
    NSString * message;
    NSString * path;
    for (id result in results)
    {
        // Figure out this result's info
        name = [result valueForKey:@"name"];
        message = [result valueForKey:@"message"];
        path = [basePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", name]];

        // Write the message to the predetermined path
        [message writeToFile:path atomically:NO];
    }
} else {
    // Error fetching results, so present error to user
    [NSApp presentError:error];
}


来源:https://stackoverflow.com/questions/2566425/export-core-data-entity-as-text-files-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!