2013-07-18 26 views
0

我需要将热点置于地图上。所有这些都与CoreData一起保存(大约25000)。 所以我需要25000注释。我还为HotSpot实体实施MKAnnotation协议。Coredata使用数据(多线程)

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

这里的是,在阵列

- (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]); 
} 
+0

看起来像你的问题是标题的方法。 ' - (NSString *)title { return self.title; }''会导致无限循环,因为'self.title'调用它被调用的方法。 – mrosales

回答

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]); 
}