views:

20

answers:

1

Hi,

I am writing an iPad application (os 3.2) which uses MKMapKit to display moving annotations on the map. Information is retrieved via XML from three different sources and collated together in my Annotation class and then displayed on the Map. This information is coming in every 5 seconds. I had this working fine for a few months with my annotations moving as required.

All the following code is a close approximation of the code (can't post my companies exact code), and it was typed in line by line so it won't compile.

My annotation code looked something like this:

@interface MyFunkyAnnotation : NSObject <MKAnnotation>
{
  CLLocationCoordinated2D coordinate;
  NSString *identifier;
  // Lots of other fields that can be displayed.
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *identifier;

// Lots of other properties as above.
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;

@end

Then in my implementation i had stuff like:

@implementation MyFunkyAnnotation

@synthesize coordinate;

-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky
{
  [self setCoordinate:[newFunky coordinate]];
  [self setIdentifier:[newFunky identifier]];
  // Lots more updating of various properties.
}
@end

This worked great, so I of course decided to redesign the whole thing and encapsulate alot of the information into other classes.

So now my code looks as follows:

@interface MyFunkyAnnotation: NSObject <MKAnnotation>
{
   SourceInfo_1  *source1;  // Contains its own coordinate property;
   SourceInfo_2  *source2;  // Contains its own coordinate property;
   SourceInfo_3  *source3;  // Contains its own coordinate property;
}
@property (nonatomic, retain) SourceInfo_1 *source1;
@property (nonatomic, retain) SourceInfo_2 *source2;
@property (nonatomic, retain) SourceInfo_3 *source3;

-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
-(CLLocationCoordinate2D)coordinate;

@end

The new implementation:

@implementation MyFunkyAnnotation

@synthesize source1, source2, source3;

-(CLLocationCoordinate2D)coordinate
{
   if ([source1 dataReliable] == YES)
   {
      return [source1 coordinate];
   }
   else if ([source2 dataReliable] == YES)
   {
      return [source2 coordinate];
   }
   else
   {
      return [source3 coordinate];
   }
 }

 -(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
 {
    if ([newFunky source1] != nil)
    {
      [self setSource1:[newFunky source1];
    }
    if ([newFunky source2] != nil)
    {
      [self setSource2:[newFunky source2];
    }
    if ([newFunky source3] != nil)
    {
      [self setSource3:[newFunky source3];
    }
 }
 @end;

Running this new code resulted in the annotations being added in the original location but they never moved at all. I am updating the coordinates inside the source1, source2, source3 when I get the XML feeds.

So after a whole day of debugging and trying various things I got it to work. I then started to remove everything I had added in during the course of the day to get the following minimal changes to make it work and the results are quite strange:

The interface is unmodified. In the implementation I added one method and added three other lines:

@implementation MyFunkyAnnotation

@synthesize source1, source2, source3;

-(void)setCoordinate:(CLLocationCoordinate2D)coordinate   //  <-- New method
{
  nil;
}

-(CLLocationCoordinate2D)coordinate
{
   if ([source1 dataReliable] == YES)
   {
      return [source1 coordinate];
   }
   else if ([source2 dataReliable] == YES)
   {
      return [source2 coordinate];
   }
   else
   {
      return [source3 coordinate];
   }
 }

 -(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
 {
    if ([newFunky source1] != nil)
    {
      [self setSource1:[newFunky source1];
      [self setCoordinate:[[newFunky source1] coordinate]];  //  <--- New Line
    }
    if ([newFunky source2] != nil)
    {
      [self setSource2:[newFunky source2];
      [self setCoordinate:[[newFunky source2] coordinate]];  //  <--- New Line
    }
    if ([newFunky source3] != nil)
    {
      [self setSource3:[newFunky source3];
      [self setCoordinate:[[newFunky source3] coordinate]];  //  <--- New Line
   }
 }
 @end;

Can anyone explain why the need to call a method which doesn't actually do anything. The "nil" was an NSLog statement so I know that it was being called. This makes absolutely no sense at all.

Any insight would be greatly appreciated.

Cheers,

Brett