Ios 多个下载进度条目标c

Ios 多个下载进度条目标c,ios,objective-c,uitableview,core-data,nsurlconnection,Ios,Objective C,Uitableview,Core Data,Nsurlconnection,我正在开发一个消息传递客户端(如Whatsapp或Line)。 我遇到了麻烦,因为我需要支持多个下载文件(和上传),在每个有当前下载过程的消息中显示一个进度条。 多重下载和上传过程已经开发好了(并且运行良好),当我试图在消息进度栏中显示进度时,问题就出现了 我的“解决方案”(我不知道是否有更好的方法)是在CoreData中的消息实体中添加一个字段“upload”和其他“downloaded”,保存上传百分比或下载百分比,并在每次NSURLConnection调用时更新该字段(上传过程中,下载中不

我正在开发一个消息传递客户端(如Whatsapp或Line)。 我遇到了麻烦,因为我需要支持多个下载文件(和上传),在每个有当前下载过程的消息中显示一个进度条。 多重下载和上传过程已经开发好了(并且运行良好),当我试图在消息进度栏中显示进度时,问题就出现了

我的“解决方案”(我不知道是否有更好的方法)是在CoreData中的消息实体中添加一个字段“upload”和其他“downloaded”,保存上传百分比或下载百分比,并在每次NSURLConnection调用时更新该字段(上传过程中,下载中不同):

但是,如果我尝试在该方法中更新CoreData上下文,我的用户界面就会冻结

好的,这就是我的问题,我将向您展示代码:

  • 这是我的核心数据消息实体:

    @property (nonatomic, retain) NSString *ident;
    @property (nonatomic, retain) NSString *localId;
    @property (nonatomic, retain) NSString *body;
    @property (nonatomic, retain) NSString *contentSize;
    @property (nonatomic, retain) NSNumber *uploaded;
    @property (nonatomic, retain) NSNumber *downloaded;
    @property (nonatomic, retain) Avatar *avatar;
    @property (nonatomic, retain) Thumbnail *thumbnail;
    
  • 这里是我管理每个HTTP连接的类,这里我尝试更新该实体

    @implementation HTTPConnection
    //SOME CODE...
    - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
    {
        if (self.localId) {
            float progress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
            [self performSelectorOnMainThread:@selector(saveProgressInDatabase:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:YES];
        }
    }
    
    - (void)saveProgressInDatabase:(NSNumber *)progress
    {
        NSManagedObjectContext *context = [[[AppDelegate alloc]init]managedObjectContext];
    
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:MESSAGE_ENTITY
                                              inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"localId = %@", self.localId];
        [fetchRequest setPredicate:predicate];
    
        NSError *coreError = nil;
        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&coreError];
    
        Message *currentMessage = [fetchedObjects objectAtIndex:0];
        [currentMessage setUploaded:progress];
    
        [context save:&coreError];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:UPLOAD_UPDATED_NOTIFICATION object:self];
    }
    
  • 在这里,我显示了消息列表,并在表视图中发送消息:

    @implementation TimelineViewController
    //SOME CODE...
    - (void)viewWillAppear:(BOOL)animated
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateTable)
                                                 name:UPLOAD_UPDATED_NOTIFICATION
                                               object:nil];
    }
    
    - (void)updateTable
    {
        NSError *error;
        [[self fetchedResultsController] performFetch:&error];
    
        if ([[_fetchedResultsController sections]count]>0) {
            [table reloadData];
    
            NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([table numberOfRowsInSection:[[_fetchedResultsController sections]count]-1] - 1) inSection:[[_fetchedResultsController sections]count]-1];
    
            [table scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
        }
    }
    
    //SOME CODE...
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Message *info = [_fetchedResultsController objectAtIndexPath:indexPath];
    
        //MORE CODE...
    
        if ([info contentSize]!=NULL) {
            [cell.progressBar setHidden:NO];
            [cell.progressBar setProgress:[[info uploaded]floatValue]];
        }
    }
    
    //CODE...
    
    - (IBAction)sendMessageAction:(id)sender
    {
        CommandServices *services = [[CommandServices alloc]init];
        [services sendMessageToGroup:message withMedia:attachedMedia withLocalId:localId];
    }
    
我不知道这是否足以理解我的问题,所以我要问两个主要问题:

  • 这是使用多个进度条管理多个下载的好方法吗?我是说,将进度保存到实体中。。。或者,我该怎么做
  • 为什么我上传或下载文件时应用程序冻结?也许我访问CoreData的次数太多了

  • 非常感谢

    我猜您应该使用以下代码来获取NSManagedObjectContext:

    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [app managedObjectContext];
    
    而不是:

    NSManagedObjectContext *context = [[[AppDelegate alloc]init]managedObjectContext];
    

    嗯……思考了几天我的问题后,我意识到我访问了太多数据库,因此我的应用程序冻结了。 我将它传递给我的HTTPConnection对象,这是我想要管理的进度条的一个实例。我只在完成时保存下载/上传进度

    这就是我的解决方案:

    在HTTPConnection中:

    + (void)setProgressBar:(UIProgressView *)progress
    {
        [downloadConnection setProgressBar:progress];
    }
    
    //CODE...
    
    - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
    {
        float progress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
        if (self.progressBar) {
            self.progressBar.progress = progress;
        }
        if (progress == 1.0) {
            [self saveProgressInDatabase:[NSNumber numberWithFloat:progress]];
        }
    }
    
    在我的邮件列表中:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        Message *info = [_fetchedResultsController objectAtIndexPath:indexPath];
    
        //MORE CODE...
    
        if (([info contentSize]!=NULL)&&([[info uploaded]floatValue]<1.0))  {
            [HTTPConnection setProgressBar:cell.progressBar];
            [cell.progressBar setHidden:NO];
        } }
    
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    Message*info=[\u fetchedResultsController objectAtIndexPath:indexPath];
    //更多代码。。。
    
    如果(([info contentSize]!=NULL)和([info upload]floatValue]我认为你在主线程中工作,请在后台线程中执行这些任务,这样你的应用程序在上传和下载时不会冻结是的,我同意你的看法。这是获取NSManagedObjectContext的更好方法,但这并不能解决我的问题。谢谢你的帮助。
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        Message *info = [_fetchedResultsController objectAtIndexPath:indexPath];
    
        //MORE CODE...
    
        if (([info contentSize]!=NULL)&&([[info uploaded]floatValue]<1.0))  {
            [HTTPConnection setProgressBar:cell.progressBar];
            [cell.progressBar setHidden:NO];
        } }