Bug 39691 - GKAgentDelegate implementors throws ArgumentException (unrecognized selector) if AgentWillUpdate is not implemented
Summary: GKAgentDelegate implementors throws ArgumentException (unrecognized selector)...
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.8 (tvOS / C7)
Hardware: PC Mac OS
: Low minor
Target Milestone: Untriaged
Assignee: Alex Soto [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-03-16 19:41 UTC by Larry O'Brien
Modified: 2016-03-23 21:01 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
UIViewController to be swapped in to "Spritekit Game" iOS template (2.42 KB, application/octet-stream)
2016-03-16 19:41 UTC, Larry O'Brien
Details
Xamarin.iOS Test Case (10.77 KB, application/zip)
2016-03-23 20:34 UTC, Alex Soto [MSFT]
Details
Xcode ObjC Test Case (25.91 KB, application/zip)
2016-03-23 20:35 UTC, Alex Soto [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Larry O'Brien 2016-03-16 19:41:42 UTC
Created attachment 15434 [details]
UIViewController to be swapped in to "Spritekit Game" iOS template

Given a GKAgentDelegate :

public class SimpleDelegate : GKAgentDelegate 
{
  public override void AgentDidUpdate(GKAgent agent)
  {
    System.Console.WriteLine("AgentDidUpdate");
  }
   
  /* Uncomment to cause runtime exception
  public override void AgentWillUpdate(GKAgent agent)
  {
    System.Console.WriteLine("AgentWillUpdate");
  }
  */ 
}

triggered via code similar to:

			agent = new GKAgent2D ();
			agent.Delegate = new SimpleDelegate ();
			
			entity = new GKEntity();
			entity.AddComponent(agent);

			agent.MaxSpeed = 10;
			var rammingSpeed = GKGoal.GetGoalToReachTargetSpeed(agent.MaxSpeed);
			agent.Behavior = GKBehavior.FromGoal(rammingSpeed, 1.0f);
			entity.Update(8.5);

creates a runtime exception (Argument exception, unknown selector, etc.)

This exception can be avoided by implementing `AgentWillUpdate` in user code. 

Repro steps:
     Create a new iOS "SpriteKit Game" from template
     Replace the generated viewcontroller with the attached (fix namespace)
     Run to create exception.

     Uncomment PositionTrackingAgent.AgentWillUpdate and run again.
     No exception
Comment 1 Larry O'Brien 2016-03-16 19:42:44 UTC
Argh. It should read "Uncomment to *avoid* runtime exception"
Comment 2 Sebastien Pouliot 2016-03-17 02:19:58 UTC
Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[bug39691_PositionTrackingDelegate agentWillUpdate:]: unrecognized selector sent to instance 0x7beba040
Native stack trace:
	0   CoreFoundation                      0x00960a14 __exceptionPreprocess + 180
	1   libobjc.A.dylib                     0x0898ee02 objc_exception_throw + 50
	2   CoreFoundation                      0x00969d63 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
	3   libobjc.A.dylib                     0x089a3196 -[NSObject forwardInvocation:] + 66
	4   CoreFoundation                      0x008a7488 ___forwarding___ + 472
	5   CoreFoundation                      0x008a728e _CF_forwarding_prep_0 + 14
	6   GameplayKit                         0x0894e24c -[GKAgent2D updateWithDeltaTime:] + 77
	7   GameplayKit                         0x0893a89b -[GKEntity updateWithDeltaTime:] + 258


It did not see it documented but looks like both selectors needs to be implemented. I get similar results if both are commented or the other one.

* They are declared as @optional in Apple's headers files and I did not see any web docs describing such requirement.

That really looks like an Apple bug and will need an ObjC sample to confirm / file rdar with them. Reducing priority (there's a workaround and it's unlikely _we_ can fix this)
Comment 3 Sebastien Pouliot 2016-03-23 15:04:05 UTC
@Alex, once bindings for watchOS 2.2 are completed can you verify (in ObjC) that this issue is an undocumented requirement from Apple ? thanks
Comment 4 Alex Soto [MSFT] 2016-03-23 20:34:28 UTC
Created attachment 15511 [details]
Xamarin.iOS Test Case

Xamarin.iOS Test Case
Comment 5 Alex Soto [MSFT] 2016-03-23 20:35:55 UTC
Created attachment 15512 [details]
Xcode ObjC Test Case

I can reproduce the same issue in this ObjC test case which is a line by line port of the C# one

Native Exception log:
https://gist.github.com/dalexsoto/6faf9cd5244c9c2d7f61
Comment 6 Alex Soto [MSFT] 2016-03-23 21:01:47 UTC
Opened radar://25322551 with apple and it is being tracked in our trello board