Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ObjectiveC-CoreData的平面文件_Objective C_Core Data_Flat File - Fatal编程技术网

Objective c ObjectiveC-CoreData的平面文件

Objective c ObjectiveC-CoreData的平面文件,objective-c,core-data,flat-file,Objective C,Core Data,Flat File,我有一个100万行的大文件。文件的设置如下所示: A1 B1 C1 D1 E1 A2 B2 C2 D2 E2 A3 B3 C3 D3 E3 我想将这些数据读入由CoreData管理的SQLite数据库 最有效的方法是什么 这可以通过使用CoreData&Objective-C或创建一个SQLite数据库来实现,然后将该数据库导入到使用CoreData的项目中。如果这不是您需要立即执行的操作,那么我可能会编写一个脚本:(1)创建数据库/模式,然后(2)从文件中读取并插入到相应的表中。然后我会将数据

我有一个100万行的大文件。文件的设置如下所示:

A1 B1 C1 D1 E1
A2 B2 C2 D2 E2
A3 B3 C3 D3 E3

我想将这些数据读入由CoreData管理的SQLite数据库

最有效的方法是什么


这可以通过使用CoreData&Objective-C或创建一个SQLite数据库来实现,然后将该数据库导入到使用CoreData的项目中。

如果这不是您需要立即执行的操作,那么我可能会编写一个脚本:(1)创建数据库/模式,然后(2)从文件中读取并插入到相应的表中。然后我会将数据库导入到项目中。

如果这不是您需要立即执行的操作,那么我可能会编写某种脚本:(1)创建数据库/架构,然后(2)读取文件并插入适当的表中。然后,我将数据库导入到项目中。

以下是我在iOS应用程序中使用的一个方法示例,用于加载一个带分隔符的文本文件(使用管道“|”作为字段分隔符),其中包含有关几种植物和动物物种的数据。该文件存储在应用程序的文档目录中,因此用户可以通过iTunes的文档共享添加/更改该文件。该文件名为“X.specieslist”,其中X与核心数据管理的sqlite数据库同名。代码检查给定的物种是否已经存在于数据库中(基于“代码”键),并且只导入那些不存在的物种

- (void)loadSpecies {

    //get path to species file stored in Documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSPersistentStoreCoordinator *psc = [managedObjectContext persistentStoreCoordinator];
    NSArray *psa = [psc persistentStores];
    NSURL *psurl = [[psa objectAtIndex: 0] URL];
    NSString *filestr = [[psurl lastPathComponent] stringByReplacingOccurrencesOfString: @"sqlite" withString: @"specieslist"] ;
    NSString *filePath = [basePath stringByAppendingPathComponent: filestr];

    //create array with each element containing a single line from the input file
    NSArray *speciesRecords = [[NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil] componentsSeparatedByString: @"\n"];

    if (speciesRecords) {
        int speciesAdded = 0;
        int speciesSkipped = 0;
        NSEntityDescription *entity = [NSEntityDescription entityForName: @"Species" inManagedObjectContext: managedObjectContext];
        NSManagedObject *newSpecies;
        NSDictionary *speciesDictionary;
        NSArray *fieldKeys = [[NSArray alloc] initWithObjects: @"code", @"scientificName", @"commonName", @"family", @"lifeform", @"growthForm", @"lifecycle", @"nativity", @"notes", nil];
        NSArray *fieldValues;
        NSArray *existingRecords = [fetchedResultsController fetchedObjects];
        NSPredicate *speciesCodePred;
        NSArray *speciesMatch;

        for (NSString *species in speciesRecords) {
            fieldValues = [species componentsSeparatedByString: @"|"];

            //check if species code already exists
            speciesCodePred = [NSPredicate predicateWithFormat: @"code MATCHES %@", [fieldValues objectAtIndex: 0] ];
            speciesMatch = [existingRecords filteredArrayUsingPredicate: speciesCodePred];

            //only add species not already present
            if ([speciesMatch count] == 0) {
                newSpecies = [[NSManagedObject alloc] initWithEntity: entity insertIntoManagedObjectContext: managedObjectContext];
                speciesDictionary = [NSDictionary dictionaryWithObjects: fieldValues forKeys: fieldKeys];
                [newSpecies setValuesForKeysWithDictionary: speciesDictionary];
                [newSpecies release];
                speciesAdded++;
            }
            else
                speciesSkipped++;
        }
        [fieldKeys release];
        NSString *speciesSkippedString = (speciesSkipped > 0) ? [NSString stringWithFormat: @" and %d species were skipped because they were already present.", speciesSkipped] : @".";

        UIAlertView *av = [[UIAlertView alloc] initWithTitle: @"Species List Import" message: [NSString stringWithFormat: @"%d species were added to the database%@", speciesAdded, speciesSkippedString] delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
        [av show];
        [av release];
    }

    else {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle: @"Species List Import" message: @"A species list file was not present, no species were imported." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [av show];
        [av release];
    }
}

下面是我在iOS应用程序中使用的加载分隔文本文件(使用管道“|”作为字段分隔符)的方法示例,该文件包含有关几种植物和动物物种的数据。该文件存储在应用程序的文档目录中,因此用户可以通过iTunes的文档共享添加/更改该文件。该文件名为“X.specieslist”,其中X与核心数据管理的sqlite数据库同名。代码检查给定的物种是否已经存在于数据库中(基于“代码”键),并且只导入那些不存在的物种

- (void)loadSpecies {

    //get path to species file stored in Documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSPersistentStoreCoordinator *psc = [managedObjectContext persistentStoreCoordinator];
    NSArray *psa = [psc persistentStores];
    NSURL *psurl = [[psa objectAtIndex: 0] URL];
    NSString *filestr = [[psurl lastPathComponent] stringByReplacingOccurrencesOfString: @"sqlite" withString: @"specieslist"] ;
    NSString *filePath = [basePath stringByAppendingPathComponent: filestr];

    //create array with each element containing a single line from the input file
    NSArray *speciesRecords = [[NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil] componentsSeparatedByString: @"\n"];

    if (speciesRecords) {
        int speciesAdded = 0;
        int speciesSkipped = 0;
        NSEntityDescription *entity = [NSEntityDescription entityForName: @"Species" inManagedObjectContext: managedObjectContext];
        NSManagedObject *newSpecies;
        NSDictionary *speciesDictionary;
        NSArray *fieldKeys = [[NSArray alloc] initWithObjects: @"code", @"scientificName", @"commonName", @"family", @"lifeform", @"growthForm", @"lifecycle", @"nativity", @"notes", nil];
        NSArray *fieldValues;
        NSArray *existingRecords = [fetchedResultsController fetchedObjects];
        NSPredicate *speciesCodePred;
        NSArray *speciesMatch;

        for (NSString *species in speciesRecords) {
            fieldValues = [species componentsSeparatedByString: @"|"];

            //check if species code already exists
            speciesCodePred = [NSPredicate predicateWithFormat: @"code MATCHES %@", [fieldValues objectAtIndex: 0] ];
            speciesMatch = [existingRecords filteredArrayUsingPredicate: speciesCodePred];

            //only add species not already present
            if ([speciesMatch count] == 0) {
                newSpecies = [[NSManagedObject alloc] initWithEntity: entity insertIntoManagedObjectContext: managedObjectContext];
                speciesDictionary = [NSDictionary dictionaryWithObjects: fieldValues forKeys: fieldKeys];
                [newSpecies setValuesForKeysWithDictionary: speciesDictionary];
                [newSpecies release];
                speciesAdded++;
            }
            else
                speciesSkipped++;
        }
        [fieldKeys release];
        NSString *speciesSkippedString = (speciesSkipped > 0) ? [NSString stringWithFormat: @" and %d species were skipped because they were already present.", speciesSkipped] : @".";

        UIAlertView *av = [[UIAlertView alloc] initWithTitle: @"Species List Import" message: [NSString stringWithFormat: @"%d species were added to the database%@", speciesAdded, speciesSkippedString] delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
        [av show];
        [av release];
    }

    else {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle: @"Species List Import" message: @"A species list file was not present, no species were imported." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [av show];
        [av release];
    }
}

我为iPhone创建了一个旅行指南,它就是这样做的。我们创建了一个读取CSV文件的工具,并将相应的对象创建和插入到CoreData中。您可以在项目中保存生成的.sqlite文件。(将该文件添加到“复制资源”构建阶段。)我为iPhone创建了一个旅行指南,类似这样做。我们创建了一个读取CSV文件的工具,并将相应的对象创建和插入到CoreData中。您可以在项目中保存生成的.sqlite文件。(将文件添加到“复制资源”构建阶段。)