Coredata使用数据(多线程)

时间:2013-07-18 14:16:41

标签: ios multithreading core-data mapkit

我需要将热点放到地图上。所有这些都使用CoreData保存(大约25000)。 所以我需要25000个注释。我还为HotSpot Entity实现了MKAnnotation协议。

针对这种情况的选择解决方案是多线程。但是,在地图上的引脚之后,其中一些没有数据(数据)。

这是填充数组

中的数据的代码
- (void)addAnnotationsForCurrentLocation {
    NSInteger hotSpotsCount = [HotSpot MR_countOfEntities];
    self.testSpotsArray = [[NSMutableArray alloc] initWithCapacity:hotSpotsCount];
    NSInteger lastThread;
    NSInteger threads = 5;
    //calculate how much spots will be in the 
    NSInteger spotsInThread = hotSpotsCount/threads;
    //calclulate how mush threads will be in one spot
    lastThread = hotSpotsCount - spotsInThread*(threads-1);
    dispatch_queue_t firstThreadQueue = dispatch_queue_create("com.thecloud.firstThreadQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t secondThreadQueue = dispatch_queue_create("com.thecloud.secondThreadQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t thirdThreadQueue = dispatch_queue_create("com.thecloud.thirdThreadQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t forthThreadQueue = dispatch_queue_create("com.thecloud.forthThreadQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t fifthThreadQueue = dispatch_queue_create("com.thecloud.fifthThreadQueue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_group_t fillArrayGroup = dispatch_group_create();
    NSLock *arrayLock = [[NSLock alloc] init];

    dispatch_group_async(fillArrayGroup, firstThreadQueue, ^{
        [self fetchWithOffest:0 andLimit:spotsInThread andLock:arrayLock];
        DDLogInfo(@"com.thecloud.firstThreadQueue and self.testSpotsArray objects - %i", [self.testSpotsArray count]);
    });

    //Other Queue

    dispatch_group_notify(fillArrayGroup, dispatch_get_main_queue(), ^{
        [self.treeController setAnnotations:self.testSpotsArray];
    });

    dispatch_release(fillArrayGroup);
}
- (void)fetchWithOffest:(NSInteger)offset andLimit:(NSInteger)limit andLock:(NSLock *)arrayLock {
    NSFetchRequest *request = [HotSpot MR_requestAll];
    [request setFetchOffset:offset];
    [request setFetchLimit:limit];
    [request setReturnsObjectsAsFaults:NO];
    NSArray *array = [HotSpot MR_executeFetchRequest:request];
    for (int i=0; i < [array count]; i++) {
        HotSpot *spot = (HotSpot *)[array objectAtIndex:i];
        [spot convertLogitudeAndLattitudeToLocationCoordinate];
        [arrayLock lock];
        [self.testSpotsArray addObject:spot];
        [arrayLock unlock];
    }
}

在此之后,当我点击某些引脚时,没有出现带有描述的弹出窗口。 在注释数组中的每个对象中都有完整数据的任何解决方案吗?

UPD

问题解决了。我确实改变了我的实体模型。现在,当我将实体添加到注释数组而不是

-(NSString *)title {
    return self.title;
}

-(NSString *)subtitle {
    return self.spotToAddress.addressLine1;
}

-(CLLocationCoordinate2D) coordinate {
    return CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);
}

我使用直接作业

- (void)prepareAnnotation {
    _title = self.name;
    _subtitle = self.spotToAddress.addressLine1;
    _coordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);
}

1 个答案:

答案 0 :(得分:0)

问题解决了。我确实改变了我的实体模型。现在,当我将实体添加到注释数组而不是

-(NSString *)title {
    return self.title;
}

-(NSString *)subtitle {
    return self.spotToAddress.addressLine1;
}

-(CLLocationCoordinate2D) coordinate {
    return CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);
}

我使用直接作业

- (void)prepareAnnotation {
    _title = self.name;
    _subtitle = self.spotToAddress.addressLine1;
    _coordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);
}