在自己创建多个线程中执行核心数据操作以节省处理时间

时间:2015-09-04 08:33:55

标签: ios objective-c multithreading core-data parallel-processing

我已经浏览了很多关于在多个线程中执行核心数据操作但是没有好运来解决我的问题。

我的代码是这样的,我必须在每十分钟后下载一个csv文件,其中包含每个10秒的条目。解析后下载此文件,并将内容保存在数据库中,然后删除文件,如果需要,我可以从数据库中获取数据。

现在,我现在拥有超过一个月的巨大现有内容,随着时间的推移可能延续多年,执行将新文件保存到数据库并将核心数据中的对象提取到数组的巨大任务使用单个线程下载的文件导致大量处理时间。此外,app中的视图需要使用之前的所有数据进行调整(它们基本上是数量与时间的关系图)。

如何在多个线程中实现此目的并优化我的代码处理时间并将UI Blockage降至最低?

请注意:在后台线程中执行任务不是我的问题,因为我无论如何都必须根据总数据显示图表。请提供有价值的建议。

编辑:执行了一些多线程 这是新代码,

- (void) downloadFiles:(NSString *)dataPath{



__block AppDelegate *appD = (AppDelegate *)[[UIApplication sharedApplication] delegate];

backgroundMOC = [[NSManagedObjectContext alloc] init];

[backgroundMOC setPersistentStoreCoordinator:[[appD managedObjectContext] persistentStoreCoordinator]];



if (parsedDetailsDataArrayForCurrentDay) {

    parsedDetailsDataArrayForCurrentDay = nil;

}



if (parsedDetailsDataArrayForCurrentMonth) {

    parsedDetailsDataArrayForCurrentMonth = nil;

}



if (parsedDetailsDataArrayForCurrentYear) {

    parsedDetailsDataArrayForCurrentYear = nil;

}

parsedDetailsDataArrayForCurrentDay = [[NSMutableArray alloc] init];

parsedDetailsDataArrayForCurrentMonth = [[NSMutableArray alloc] init];

parsedDetailsDataArrayForCurrentYear = [[NSMutableArray alloc] init];



dispatch_group_t d_group = dispatch_group_create();





for (NSInteger i = 0; i <= (self.filesListArray.count - 1); i++) {



    NSString *filePathOnPhone = [dataPath stringByAppendingString:[NSString stringWithFormat:@"/%@", [[self.filesListArray objectAtIndex:i] objectForKey:@"kCFFTPResourceName"]]];



    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"ParsedInfiDetails"];



    NSString *nameToGet = [[self.filesListArray objectAtIndex:i] objectForKey:@"kCFFTPResourceName"];



    NSLog(@"File Check: %@", nameToGet);

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fileName = %@", nameToGet];

    [fetch setPredicate:predicate];



    NSError *error = nil;



    NSArray *results = [backgroundMOC executeFetchRequest:fetch error:&error];



    NSArray *definedResults = [results copy];

    if(definedResults && (definedResults.count !=0)) {

        NSLog(@"Entities with that name: %@", results);

        @autoreleasepool {



            NSArray *result = [[definedResults sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modDate" ascending:YES]]] copy];



            NSDate *specificDate = [NSDate date];



            NSMutableArray *sortedDateArray = [[NSMutableArray alloc] init];

            NSMutableArray *sortedDateCurrentYearArray = [[NSMutableArray alloc] init];

            NSMutableArray *sortedDateCurrentMonthArray = [[NSMutableArray alloc] init];

            for (int i = 0; i < result.count; i++) {

                NSManagedObject *obj = [result objectAtIndex:i];

                NSDate *objDate = [obj valueForKey:@"modDate"];

                NSCalendar *gregorian = [NSCalendar currentCalendar];



                NSDateComponents *components = [gregorian componentsInTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] fromDate:objDate];

                NSInteger day = [components day];

                NSInteger month = [components month];

                NSInteger year = [components year];



                NSDateComponents *specificComps = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:specificDate];

                NSInteger specificDay = [specificComps day];

                NSInteger specificMonth = [specificComps month];

                NSInteger specificYear = [specificComps year];



                if(day == 24){



                }



                if (day == specificDay && month == specificMonth && year == (specificYear-2000)) {

                    [sortedDateArray addObject:obj];

                }



                NSDate *todayDate = [NSDate date];

                NSDateComponents *componentsForToday = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:todayDate];

                NSInteger currentMonth = [componentsForToday month];

                NSInteger currentYear = [componentsForToday year];





                if (year == (currentYear-2000)) {

                    [sortedDateCurrentYearArray addObject:obj];

                }



                if (year == (currentYear -2000) && month == currentMonth) {

                    [sortedDateCurrentMonthArray addObject:obj];

                }

            }



            NSMutableArray *sortedTimedArray = [[NSMutableArray alloc] initWithArray:[sortedDateArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];



            NSMutableArray *sortedTimedCurrentYearArray = [[NSMutableArray alloc] initWithArray:[sortedDateCurrentYearArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];



            NSMutableArray *sortedTimedCurrentMonthArray = [[NSMutableArray alloc] initWithArray:[sortedDateCurrentMonthArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];







            [parsedDetailsDataArrayForCurrentDay addObjectsFromArray:sortedTimedArray];

            [parsedDetailsDataArrayForCurrentYear addObjectsFromArray:sortedTimedCurrentYearArray];

            [parsedDetailsDataArrayForCurrentMonth addObjectsFromArray:sortedTimedCurrentMonthArray];

        }

    } else {

        NSLog(@"Error: %@", error);

        NSString *threadName = [NSString stringWithFormat:@"%ld THREAD", (long)i];

        dispatch_queue_t myQueue = dispatch_queue_create([threadName UTF8String], NULL);





        dispatch_group_async(d_group, myQueue, ^{

            NSManagedObjectContext *backgroundMOC1;

            backgroundMOC1 = [[NSManagedObjectContext alloc] init];

            [backgroundMOC1 setPersistentStoreCoordinator:[[appD managedObjectContext] persistentStoreCoordinator]];

            NSLog(@"Entered Thread ");



            BOOL success = [appD.ftpManager downloadFile:[[self.filesListArray objectAtIndex:i] objectForKey:@"kCFFTPResourceName"] toDirectory:[NSURL URLWithString:dataPath] fromServer:srv];



            if (success) {

//                dispatch_group_async(d_group, myQueue, ^{

                NSMutableDictionary *dict = [appD.ftpManager progress];

                NSString *filePath = [dataPath stringByAppendingString:[NSString stringWithFormat:@"/%@", [[self.filesListArray objectAtIndex:i] objectForKey:@"kCFFTPResourceName"]]];



                CHCSVParser *parser = [[CHCSVParser alloc] initWithContentsOfCSVURL:[NSURL fileURLWithPath:filePath]];

                [parser parse];

                NSMutableArray *currentFileComponentsArray = [NSArray arrayWithContentsOfCSVURL:[NSURL fileURLWithPath:filePath]];

                NSMutableArray *parsedDetailsEntitiesArray = [[NSMutableArray alloc] init];

                for (int j = 1; j <= (currentFileComponentsArray.count-1); j++) {





                    NSArray *detailsArray = [currentFileComponentsArray objectAtIndex:j];

                    if (!(detailsArray.count < 32)) {

                        NSManagedObject *parsedDetails = [NSEntityDescription

                                                          insertNewObjectForEntityForName:@"ParsedInfiDetails"

                                                          inManagedObjectContext:[appD managedObjectContext]];

                        NSString *totalDateString = [NSString stringWithFormat:@"%@ %@", [detailsArray objectAtIndex:0], [detailsArray objectAtIndex:1]];

                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

                        dateFormatter.dateFormat = @"dd/MM/yyyy HH:mm:ss";

                        NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

                        [dateFormatter setTimeZone:gmt];

                        NSDate *startDate = [dateFormatter dateFromString:totalDateString];

                        [parsedDetails setValue:startDate forKey:@"modDate"];

                        [parsedDetails setValue:startDate forKey:@"modTime"];

                        ———————————————————————PERFORM PARSEDDETAILS STATEMENTS----------------





                        NSError *error;



                        NSLog(@"Saved File in Database: %@", [[self.filesListArray objectAtIndex:i] objectForKey:@"kCFFTPResourceName"]);

                        NSLog(@"Saved thread");

                        if (![backgroundMOC1 save:&error]) {

                            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);

                        }

                        else{

                            if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

                                //if the download fails, we try to delete the empty file created by the stream.

                                [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];

                                //when data is stored in coredata remove the downloaded file.

                            }

                            [parsedDetailsEntitiesArray addObject:parsedDetails];



                        }



                    }





                }

                NSSet *set = [[NSSet alloc] initWithArray:parsedDetailsEntitiesArray];

                [self.relevantInverId setValue:set forKey:@"infiDetails"];



                NSDate *specificDate = [NSDate date];

                @autoreleasepool {

                    NSMutableArray *sortedDateArray = [[NSMutableArray alloc] init];

                    NSMutableArray *sortedDateCurrentYearArray = [[NSMutableArray alloc] init];

                    NSMutableArray *sortedDateCurrentMonthArray = [[NSMutableArray alloc] init];



                    for (int i = 0; i < parsedDetailsEntitiesArray.count; i++) {

                        NSManagedObject *obj = [parsedDetailsEntitiesArray objectAtIndex:i];

                        NSDate *objDate = [obj valueForKey:@"modDate"];



                        NSCalendar *gregorian = [NSCalendar currentCalendar];



                        NSDateComponents *components = [gregorian componentsInTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] fromDate:objDate];

                        NSInteger day = [components day];

                        NSInteger month = [components month];

                        NSInteger year = [components year];



                        NSDateComponents *specificComps = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:specificDate];

                        NSInteger specificDay = [specificComps day];

                        NSInteger specificMonth = [specificComps month];

                        NSInteger specificYear = [specificComps year];



                        if(day == 24){





                        }

                        if (day == specificDay && month == specificMonth && year == (specificYear-2000)) {

                            [sortedDateArray addObject:obj];

                        }



                        NSDate *todayDate = [NSDate date];

                        NSDateComponents *componentsForToday = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:todayDate];

                        NSInteger currentMonth = [componentsForToday month];

                        NSInteger currentYear = [componentsForToday year];





                        if (year == (currentYear-2000)) {

                            [sortedDateCurrentYearArray addObject:obj];

                        }



                        if (year == (currentYear-2000) && month == currentMonth) {

                            [sortedDateCurrentMonthArray addObject:obj];

                        }



                    }



                    NSMutableArray *sortedTimedArray = [[NSMutableArray alloc] initWithArray:[sortedDateArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];



                    [parsedDetailsDataArrayForCurrentDay addObjectsFromArray:sortedTimedArray];





                    NSMutableArray *sortedTimedCurrentYearArray = [[NSMutableArray alloc] initWithArray:[sortedDateCurrentYearArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];



                    NSMutableArray *sortedTimedCurrentMonthArray = [[NSMutableArray alloc] initWithArray:[sortedDateCurrentMonthArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];







                    [parsedDetailsDataArrayForCurrentYear addObjectsFromArray:sortedTimedCurrentYearArray];

                    [parsedDetailsDataArrayForCurrentMonth addObjectsFromArray:sortedTimedCurrentMonthArray];

                }




//                                    });



            }



        });



    }







    BOOL isFileAlreadyPresent = [[NSFileManager defaultManager] fileExistsAtPath:filePathOnPhone];





}



NSMutableArray *sortedParsedDetailsArrayForCurrentDay = [[NSMutableArray alloc] initWithArray:[parsedDetailsDataArrayForCurrentDay sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"modTime" ascending:YES]]]];



NSDate *startDate ;

NSDate *endaDate;





dispatch_group_notify(d_group, dispatch_get_main_queue(), ^{

    dispatch_async(dispatch_get_main_queue(), ^{

        [self stopLogoSpin];

        [hud dismiss];



    });

    NSLog(@"All background tasks are done!!");

});


}

现在,在日志中可以看到日志@“输入的线程”但是,@“数据库中保存的文件:%@”,[[self.filesListArray objectAtIndex:i]和@“保存的线程”不会被调用。

另外,

BOOL success = [appD.ftpManager downloadFile:[[self.filesListArray objectAtIndex:i] objectForKey:@"kCFFTPResourceName"] toDirectory:[NSURL URLWithString:dataPath] fromServer:srv];

有助于在下载方法中使用其单独的线程下载文件。 应该在下面取消注释吗?

    //                dispatch_group_async(d_group, myQueue, ^{

有意使主线程等待,但下面的这个块也永远不会被调用:

dispatch_group_notify(d_group, dispatch_get_main_queue(), ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self stopLogoSpin];
        [radialHUD dismiss];

    });
    NSLog(@"All background tasks are done!!");
});

请建议对上述问题进行任何修改和解决方案。

1 个答案:

答案 0 :(得分:0)

通过在循环外部移动所有不需要的内容并使用GCD执行循环操作来优化代码。

 dispatch_queue_t myOwnQueue = dispatch_queue_create([@"MyOwnQueue" UTF8String], NULL);

dispatch_apply(self.filesListArray.count, myOwnQueue, ^(size_t i) {   });

这导致时间减少一半。然而,需要进一步优化。