gpt4 book ai didi

ios - 这是 IOS 中 CoreData 应用程序的良好设计吗?

转载 作者:行者123 更新时间:2023-11-28 21:48:18 25 4
gpt4 key购买 nike

这是我尝试使用 CoreData 开发的第一个应用程序。到目前为止,我采用的是一种懒惰的方法,我想问一下我的想法是否有道理。

所以,这是我所知道的:

对于所有 tableviewDataSource 委托(delegate)方法,我有这样的东西:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self numberOfActiveLeagues];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

return [self fetchSectionTitleForId:[NSNumber numberWithInteger:section+1]];
}

同样适用于

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

因此,我调用的每个方法都会生成一个 NSFetchRequest 并获取数据。例如,看看这两个:

-(NSString*) fetchSectionTitleForId:(NSNumber*)leagueId
{
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entityGW = [NSEntityDescription entityForName:@"League" inManagedObjectContext:self.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"leagueId", leagueId];
[fetchRequest setEntity:entityGW];
[fetchRequest setPredicate:predicate];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

NSError *fetchError = nil;
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];

if (!fetchError) {
for (NSManagedObject *managedObject in result) {

return [NSString stringWithFormat:@"%@ , GW : %@",[managedObject valueForKey:@"leagueName"],[[managedObject valueForKey:@"gameweeks"] valueForKey:@"gwId"]];
}

} else {
NSLog(@"Error fetching data.");
NSLog(@"%@, %@", fetchError, fetchError.localizedDescription);
return @" ";
}
return @" ";
}

-(NSInteger) numberOfActiveLeagues
{
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entityLeague = [NSEntityDescription entityForName:@"League" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityLeague];


NSError *fetchError = nil;
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];

if (!fetchError) {
return [result count];
} else {
NSLog(@"Error fetching data.");
NSLog(@"%@, %@", fetchError, fetchError.localizedDescription);
return 0;
}
}

现在,在我看来,这样做太过分了——方法和对我的数据的访问应该执行很多次。

那么,第一个问题是这个设计有多糟糕?

我曾想过这样做:

有一些变量,比方说:

@property (strong, nonatomic) NSArray *sectionTitles;

并在 viewDidLoad 期间为它们赋值。其他人也一样。此外,我将确保每次我的 CoreData 需要更新时,我都会更新我的本地变量并重新加载我的表数据。

这样更好吗?我有什么收获吗?

如果不是,获取核心数据并将数据呈现给 TableView 的最佳方法是什么?我需要有多个 NSFetchedControllers,并且根据时间戳,我可能需要从服务器获取新数据,或者删除我的一些核心数据条目等。

最佳答案

So, 1st question is how bad design is this?

非常,但还是有希望的。使用您当前的代码,您正在执行 2*(节数)+ 1 次获取以获取一次获取中可以获得的数据。提取是相对昂贵(缓慢)的操作,因此您应该尽量减少提取次数。在 TableView 的回调中执行提取可能会导致 UI 性能不佳。

您对 sectionTitles 数组进行一次提取的想法要好得多。如果您已经在使用 NSFetchedResultsController,您可以使用 sectionNameKeyPath: 进一步改进。然后您可以从 NSFetchedResultsController 中获取部分信息,而无需进行单独的提取。

关于ios - 这是 IOS 中 CoreData 应用程序的良好设计吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242391/

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