2014-02-18 27 views
1

之後,我試着問兩次,並解釋自己,我做了挖,我想現在我可以解釋我的問題好:我使用的NSManagedObjectContext做工精良只有幾次,然後就停止工作

1)核心數據保存到NSManagedObjectsCoreDataTrap & CoreDataAllTraps。 第一次,我解析一個大的XML,然後轉換爲數組,然後將詳細信息添加到CoreDataAllTraps,這個操作進行得很好,因爲我知道,因爲我做了一個日誌。

2)然後,爲了測試,我提取所有記錄並記錄它們的總數。

這些函數給我正確的記錄數。 3)然後只是初始化一些變量。

4)然後初始化我的四叉樹。

這讓我回到我的斷言錯誤。

Error: fetchedObjects have no records.

5)然後,所有其他使用核心數據的函數當然會給我帶來錯誤,因爲沒有數據。

相關(編號)代碼:

1: 
--- 
- (void)addOrUpdateTrap:(Traps*)trapObject 
{ 
    NSManagedObjectContext *context = generateManagedObjectContext(); 

    int trapID = trapObject.getTrapID; 
    CoreDataAllTraps *trapEntity = nil; 
    NSError *error = nil; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kCORE_DATA_ALL_TRAPS_ENTITY]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"trapID == %d", trapID]; 
    [fetchRequest setPredicate:predicate]; 

    NSArray *results = [context executeFetchRequest:fetchRequest error:&error]; 
    if (results == nil) 
    { 
     // Handle error 
    } 
    else if (results.count == 0) 
    { 
     // Nothing to update, add new trap 
     // Create a new record (row) 
     trapEntity = [NSEntityDescription insertNewObjectForEntityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:context]; 
    } 
    else 
    { 
     trapEntity = results[0]; // There should be only one object for the ID. 
    } 


    if (trapEntity != nil) 
    { 
     // Set properties for new or existing object ... 

     // Int 
     [trapEntity setTrapID:[NSNumber numberWithInt:trapObject.getTrapID]]; 
     [trapEntity setType:[NSNumber numberWithInt:trapObject.getTrapType]]; 
     [trapEntity setDist:[NSNumber numberWithInt:trapObject.getTrapDistanceToCar]]; 
     [trapEntity setDist_to_close_point:[NSNumber numberWithInt:trapObject.getTrapDistanceToClosePoint]]; 
     [trapEntity setActive:[NSNumber numberWithInt:trapObject.isActive]]; 
     [trapEntity setAlert:[NSNumber numberWithInt:trapObject.isAlert]]; 
     [trapEntity setAlarmDistance:[NSNumber numberWithInt:trapObject.alarmDistance]]; 
     [trapEntity setRoadNumber:[NSNumber numberWithInt:trapObject.roadNumber]]; 
     [trapEntity setPolys:[NSNumber numberWithInt:trapObject.polygons]]; 
     [trapEntity setEnter_to_area:[NSNumber numberWithInt:trapObject.getTrapEnterToArea]]; 

     // Double 
     [trapEntity setLat:[NSNumber numberWithDouble:trapObject.getTrapLat]]; 
     [trapEntity setLon:[NSNumber numberWithDouble:trapObject.getTrapLon]]; 
     [trapEntity setClose_point_lat:[NSNumber numberWithDouble:trapObject.getTrapClosePointLat]]; 
     [trapEntity setClose_point_lon:[NSNumber numberWithDouble:trapObject.getTrapClosePointLon]]; 

     // NSString 
     [trapEntity setLastTrapAlarm:[NSString stringWithFormat:@"%li", trapObject.getTrapLastAlarm]]; 
     [trapEntity setPoly0:trapObject.getTrapPolygonA]; 
     [trapEntity setPoly1: trapObject.getTrapPolygonB]; 
     [trapEntity setPoly2: trapObject.getTrapPolygonC]; 
     [trapEntity setPolygonAzimut1: trapObject.getTrapPolygonAzimuthA]; 
     [trapEntity setPolygonAzimut2: trapObject.getTrapPolygonAzimuthB]; 
     [trapEntity setPolygonAzimut3: trapObject.getTrapPolygonAzimuthC]; 
     [trapEntity setDesc: trapObject.getTrapDesc]; 
     // etc. for all properties ... 


     error = nil; 
     if ([context save:&error] == NO) { 
      NSLog(@"%s error saving: %@\n%@", __PRETTY_FUNCTION__, error.localizedDescription, error.userInfo); 
     } 
     else { 
      [context reset]; 
     } 
    } 
} 

2: 
--- 
- (void)saveArray:(NSArray*)array 
{ 
    kNETROADS_CONTEXT.arrayOfAllTraps = self.arrayOfAllTraps = array.mutableCopy; 
    NSLog(@"Total number of traps: %d", self.arrayOfAllTraps.count); 

    NSManagedObjectContext *context = generateManagedObjectContext(); 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSError *error = nil; 
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    NSLog(@"TrapService - fetchedObjects.count: %d", fetchedObjects.count); 

    if (fetchedObjects == nil || fetchedObjects.count == 0) { 
     NSLog(@"saveArray - localizedDescription: %@, userInfo: %@", error.localizedDescription, error.userInfo); 
    } 

    [self readArray]; 
} 

- (void)readArray 
{ 
    NSManagedObjectContext *context = generateManagedObjectContext(); 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSError *error = nil; 
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    NSLog(@"readArray - fetchedObjects.count: %d", fetchedObjects.count); 

    if (fetchedObjects == nil || fetchedObjects.count == 0) { 
     NSLog(@"readArray - localizedDescription: %@, userInfo: %@", error.localizedDescription, error.userInfo); 
    } 
} 

3: 
--- 
- (void)initVariables 
{ 
    db = [[DataBase alloc] init]; 

    //dbat = [[DataBaseAllTraps alloc] init]; 
    dbat = [DataBaseAllTraps getInstance]; 

    kRECEIVER_CONTEXT.db = [[DataBase alloc] init]; 

    [db deleteTrapsTable]; 
    [dbat deleteTrapsTable]; 

    self.dictAddUserLocations = [[NSMutableDictionary alloc] init]; 
    self.arrayOfAllTraps = [Netroads sharedInstance].arrayOfAllTraps; 
    self.arrayOfLocations = [[NSMutableArray alloc] init]; 
    self.firstOnLocationChanged = YES; 
    self.mLocation = [CLLocation new]; 
    self.mLastLocation = [CLLocation new]; 
    self.globalLocation = [CLLocation new]; 
    self.lastGlobalLocation = [CLLocation new]; 
    self.myLocations = [[NSMutableArray alloc] init]; 
    self.accuracy = @"N/A"; 
    self.closeTrap = [[Traps alloc] init]; 

    self.notification = [NSNotificationCenter defaultCenter]; 
    // [self.notification addObserver:self selector:@selector(onReceive:) name:kSend_To_Receiver_Notification object:nil]; 
} 

4: 
--- 

- (void)initializeQuadTree 
{ 
    self.qTree = [[QuadTree alloc] init]; 
    BOOL success = YES; 

    NSManagedObjectContext *context = generateManagedObjectContext(); 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 

    NSError *error = nil; 
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    if (fetchedObjects == nil || fetchedObjects.count == 0) 
    { 
     NSLog(@"initializeQuadTree - localizedDescription: %@, userInfo: %@", error.localizedDescription, error.userInfo); 
     success = NO; 
    } 

    NSLog(@"initializeQuadTree - fetchedObjects.count: %d", fetchedObjects.count); 

    NSAssert(fetchedObjects != nil, @"Error: fetchedObjects = nil"); 
    NSAssert(fetchedObjects.count > 0, @"Error: fetchedObjects have no records."); 

    if (success) 
    { 
     for (CoreDataAllTraps *trap in fetchedObjects) 
     { 
      double latitude = trap.lat.doubleValue; 
      double longitude = trap.lon.doubleValue; 

      double closePointLat = trap.close_point_lat.doubleValue; 
      double closePointLon = trap.close_point_lon.doubleValue; 

      DummyAnnotation *trapAnnotation = [[DummyAnnotation alloc] init]; 

      if (closePointLat != 0.0 || closePointLon != 0.0) trapAnnotation.coordinate = CLLocationCoordinate2DMake(closePointLat, closePointLon); 
      else trapAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude); 

      [self.qTree insertObject:trapAnnotation]; 
     } 
    } 
    else 
    { 
     for (Traps *trap in kNETROADS_CONTEXT.arrayOfAllTraps) 
     { 
      double latitude = trap.lat; 
      double longitude = trap.lon; 

      double closePointLat = trap.closePointLat; 
      double closePointLon = trap.closePointLon; 

      DummyAnnotation *trapAnnotation = [[DummyAnnotation alloc] init]; 

      if (closePointLat != 0.0 || closePointLon != 0.0) trapAnnotation.coordinate = CLLocationCoordinate2DMake(closePointLat, closePointLon); 
      else trapAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude); 

      [self.qTree insertObject:trapAnnotation]; 
     } 
    } 
    NSLog(@"TOTAL NUMBER OF TRAPS (%s): %i", __PRETTY_FUNCTION__, success?fetchedObjects.count:[Netroads sharedInstance].arrayOfAllTraps.count); 
} 

旁註: *調用initializeQuadTree之後,我的初始化外景經理。 *在位置管理器中,我有一個dispatch_async,它包含了整個代碼。 *完成全部初始化後,主要代碼通過位置管理器didUpdateLocations發生。 *對於核心數據的每一個使用我生成新NSManagedObjectConext這樣的:

FOUNDATION_EXPORT NSManagedObjectContext *generateManagedObjectContext() 
{ 
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; 
    context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator; 
    return context; 
} 

回答

2
[db deleteTrapsTable]; 
[dbat deleteTrapsTable]; 

後您刪除的表創建

+1

啊哈!這是我的問題,我把初始化變量的函數移到頂端,現在它的工作。 –