gpt4 book ai didi

ios - 如何清除 Restkit 中 UITableView 的 NSFetchedResultsController 结果

转载 作者:行者123 更新时间:2023-11-29 03:12:38 25 4
gpt4 key购买 nike

我有一个主UITableView程序,单击的每个单元格都会导致另一个UITableView:相关情节到单击的程序。

问题:每次我单击节目单元格时,它都会将我单击的最后一个节目中的剧集附加到剧集 TableView Controller 中。

示例:假设节目 A 有剧集 i、ii,则点击剧集 A 在 tableView 上显示 i、ii假设节目 B 有第 iii 集,那么点击第 B 集将显示 i、ii、iii 而不仅仅是 iii

我尝试过的:我确保每次单击程序单元时我总是从头开始实例化 EpisodesDataSource,并且每次单击程序单元时我都会销毁 NSFetchedResultsController 实例。我还确保不会多次添加相同的响应描述符..并且我还使用了

[NSFetchedResultsController deleteCacheWithName:@"Master"];

每次我调用fetchedResultsController..但仍然没有运气..请参阅下面的实现细节。

ProgramsViewController中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Controller *controller = [Controller getSingleton];

MediaListViewController *episodesVC =
[controller.storyboard instantiateViewControllerWithIdentifier:@"NewsReportsViewController"];
ProgramCell *programCell = (ProgramCell *)[tableView cellForRowAtIndexPath:indexPath];

episodesVC.dataSource = [[EpisodesDataSource alloc] initWithProgramId:programCell.programId];

[self.navigationController episodesVC animated:YES];
}

EpisodesDataSource

@implementation EpisodesDataSource

- (id)initWithProgramId:(NSNumber *)programId {
self = [self init];
self.programId = programId;
self.parameters = @{@"pid": programId};
self.predicate = @"episode";
return self;
}

- (id)init {
self = [super init];
self.entityName = @"Episodes";
self.resourcePath = kEpisodesPath;
self.sortKey = @"episode_title";
self.keyPath = @"EpisodesList.Episode";
return self;
}

- (void)addResponseDescriptor {

Model *model = [Model getSingleton];
NSArray *existingResponseDescriptors = [model.objectManager.responseDescriptors mapObjectsUsingBlock:^(RKResponseDescriptor *descriptor, NSUInteger idx){
return [descriptor keyPath];
}];

if ([existingResponseDescriptors containsObject:self.keyPath]) {
NSLog(@"response descriptor %@ already exsits! return", self.keyPath);
return;
}

NSLog(@"adding response descriptor %@", self.keyPath);
RKEntityMapping *episodesEntityMapping = [RKEntityMapping mappingForEntityForName:self.entityName inManagedObjectStore:model.managedObjectStore];
// left is http, right is dbase
[episodesEntityMapping addAttributeMappingsFromDictionary:@{@"programId.text": @"episode_id",
@"EpisodeTitle.text": @"episode_title",
@"EpisodeImageUrl.text": @"episode_image_url",
@"EpisodeNewsUrl.text": @"episode_news_url",
@"EpisodeVideoUrl.text": @"episode_video_url",
@"EpisodeVideoId.text": @"episode_video_id",
@"EpisodeCaption.text": @"episode_caption",
@"EpisodeDetails.text": @"episode_details",
}];

episodesEntityMapping.identificationAttributes = @[ @"episode_id" ];


RKResponseDescriptor *episodesResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:episodesEntityMapping
method:RKRequestMethodAny
pathPattern:self.resourcePath
keyPath:self.keyPath
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[model.objectManager addResponseDescriptor:episodesResponseDescriptor];

}

MediaListViewController中:

- (void)viewDidLoad
{
[super viewDidLoad];

// setup delegates
NSAssert(dataSource, @"Datasource of MediaListViewController cannot be nil");
dataSource.tableViewController = self;
self.tableView.dataSource = dataSource;

// setup refresh controller
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;

// load data
[dataSource addResponseDescriptor];
[dataSource downloadData];
Controller *controller = [Controller getSingleton];
UIView *background = [[UIView alloc] initWithFrame:controller.window.frame];
[background setBackgroundColor:[UIColor redColor]];
[background setTag:999];
[[self.tableView superview] addSubview:background];
[[self.tableView superview] sendSubviewToBack:background];
}

EpisodesDataSource 继承自 DataSource:,它具有:

- (NSFetchedResultsController *)fetchedResultsController
{
// reset
[NSFetchedResultsController deleteCacheWithName:@"Master"];

if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}

NSManagedObjectContext *context =((Model *)[Model getSingleton]).managedObjectStore.mainQueueManagedObjectContext;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName
inManagedObjectContext:context];
[fetchRequest setEntity:entity];

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

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:self.sortKey ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController
= [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _fetchedResultsController;
}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[tableViewController.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[tableViewController.tableView endUpdates];
}

最佳答案

根据 @Duncan Groenewald 的评论,您应该使用谓词来过滤数据存储的内容,以便仅显示一个子集。目前,每次处理新的下载时,您都会显示所有内容,您会获得额外的表格 View 内容。


对于后续问题,请检查谓词。当您期望匹配时(即将字符串“100”与数字 100 进行比较),托管对象和谓词中使用的类/数据类型不匹配可能会导致失败。

通常,托管对象中的所有相同类型的信息都应该在数据类型上匹配,并且当从主接口(interface)转移到详细接口(interface)时,您应该传递托管对象(或来自托管对象的值)以确保保持一致性获取/过滤时。

关于ios - 如何清除 Restkit 中 UITableView 的 NSFetchedResultsController 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22064978/

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