gpt4 book ai didi

ios - iOS数据导入策略/设计

转载 作者:行者123 更新时间:2023-12-01 17:01:13 27 4
gpt4 key购买 nike

我正在为iPad应用程序编写数据导入例程。它似乎工作正常。但是,由于我是IOS菜鸟,并且来自应用程序开发背景,在这种应用程序开发背景中,这些任务是通过对SQL Server的调用来处理的,因此,我觉得需要仔细检查是否使用了正确的方法。

这是我用来将数据导入主数据实体之一的基本方法:

// SET UP SOME VARIABLES/VALUES APPLICABLE TO ALL IMPORTS

NSStringEncoding encoding = 1;

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];

NSString* file = nil;

NSError* error = nil;

NSArray* records = nil;

NSArray* fields = nil;

NSPredicate* predicate = nil;

NSArray* filteredArray = nil;

// IMPORT GEO DATA
// ProvinceState

file = [[NSBundle bundleForClass:[self class]] pathForResource:@"ProvinceState" ofType:@"txt"];

error = nil;

records = [NSArray arrayWithContentsOfCSVFile:file usedEncoding:&encoding error:&error];

NSManagedObject* provinceState = nil;

NSMutableArray* provinceStateArray = [NSMutableArray arrayWithCapacity:[records count]];

for (int i = 0; i < [records count]; ++i)
{
fields = [records objectAtIndex:i];

if ([fields count] == 2)
{

provinceState = [NSEntityDescription insertNewObjectForEntityForName:@"ProvinceState" inManagedObjectContext:self.managedObjectContext];

[provinceState setValue: [numberFormatter numberFromString:[fields objectAtIndex:0]] forKey:@"id"];

[provinceState setValue:[fields objectAtIndex:1] forKey:@"name"];

[provinceStateArray addObject:provinceState];

}
}

几个关键点。这个特定的实体非常简单-只是一个整数id和一个字符串名称。它的源表是一个简单的csv文件。 arrayWithContentsOfCSVFile:usedEncoding:error:方法来自Dave Delong出色的CHCSV解析器,它使解析变得轻而易举。我尚未应用任何错误处理,因为我正在尝试首先研究基本技术。

好的,除非您发现此简单代码有任何问题,否则我将继续我的主要问题。上面的代码的最后一行中引用的provinceStateArray是为了省去以后在需要将主数据实体填充到事务数据的关系时查询主数据实体的麻烦。我意识到这是:a)增加了我的内存需求,并且b)暗含了阻止我耗尽关联的autoreleasepool的功能,但是由于半打左右的主数据实体总共只包含数百个小对象,所以我不认为这是一个问题。有人有不同的看法吗?

现在,有关我的部分。在以下填充位置实体的代码中,我使用这些相同的主数据实体数组,如下所示:
file = [[NSBundle bundleForClass:[self class]] pathForResource:@"Location" ofType:@"txt"];

error = nil;

records = [NSArray arrayWithContentsOfCSVFile:file usedEncoding:&encoding error:&error];

NSManagedObject* location = nil;

NSMutableArray* locationArray = [NSMutableArray arrayWithCapacity:[records count]];

for (int i = 0; i < [records count]; ++i)
{
NSArray* fields = [records objectAtIndex:i];

if ([fields count] == 5)
{

location = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:self.managedObjectContext];

[location setValue: [numberFormatter numberFromString:[fields objectAtIndex:0]] forKey:@"id"];

predicate = [NSPredicate predicateWithFormat:@"id = %@", [numberFormatter numberFromString:[fields objectAtIndex:1]]];

filteredArray = [provinceStateArray filteredArrayUsingPredicate:predicate];

if ([filteredArray count] > 0)
{
[location setValue:[filteredArray objectAtIndex:0] forKey:@"provinceState"];
}

...依次类推,直到必须设置的其他关系实体为止。

这就是我必须感到很陌生的部分-必须构造大量对象以设置关系,即使我没有其他选择。

另外,由于源数据来自RDBMS,因此我填充了一对多关系的一侧,而不是遍历集合。

所有这些都有什么难闻的气味,或者我能以我所理解的舒适感继续进行吗?

最佳答案

我认为最好创建SQLite数据库,将其导入到您的项目中,然后将数据从文件导入到该数据库(它是硬盘驱动器内存),而不是RAM中的数组。然后,当您的数据库准备就绪可以使用时,对其进行查询以仅将用户所需的数据填充到iPhone RAM中。

关于ios - iOS数据导入策略/设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6716060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com