Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NSObject] Unify some code between iOS and macOS. #15302

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 1 addition & 37 deletions src/Foundation/NSObject.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,15 @@

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

using UIKit;
using ObjCRuntime;

namespace Foundation {
public partial class NSObject : INativeObject
#if !COREBUILD
, IDisposable
#endif
{
public partial class NSObject {
#if !COREBUILD

#if !NET && !WATCH
[Obsolete ("Use 'PlatformAssembly' for easier code sharing across platforms.")]
public readonly static Assembly MonoTouchAssembly = typeof (NSObject).Assembly;
#endif

public static NSObject Alloc (Class kls) {
var h = Messaging.IntPtr_objc_msgSend (kls.Handle, Selector.GetHandle (Selector.Alloc));
return new NSObject (h, true);
}

public void Init () {
if (handle == IntPtr.Zero)
throw new Exception ("you have not allocated the native object");

handle = Messaging.IntPtr_objc_msgSend (handle, Selector.GetHandle ("init"));
}

public static void InvokeInBackground (Action action)
{
// using the parameterized Thread.Start to avoid capturing
// the 'action' parameter (it'll needlessly create an extra
// object).
new System.Threading.Thread ((v) =>
{
((Action) v) ();
})
{
IsBackground = true,
}.Start (action);
}
#endif // !COREBUILD
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/Foundation/NSObject.mac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using ObjCRuntime;

namespace Foundation {
public partial class NSObject : INativeObject
#if !COREBUILD
, IDisposable
#endif
{
public partial class NSObject {
#if !COREBUILD

// note: the linker will remove the unrequired `dlopen` calls
Expand Down
33 changes: 31 additions & 2 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ public class NSObjectFlag {
[SupportedOSPlatform ("tvos")]
#endif
[StructLayout (LayoutKind.Sequential)]
public partial class NSObject
public partial class NSObject : INativeObject
#if !COREBUILD
: IEquatable<NSObject>
, IEquatable<NSObject>
, IDisposable
#endif
{
#if !COREBUILD
Expand Down Expand Up @@ -1045,6 +1046,34 @@ public IDisposable AddObserver (NSString key, NSKeyValueObservingOptions options
AddObserver (o, key, options, o.Handle);
return o;
}

public static NSObject Alloc (Class kls)
{
var h = Messaging.IntPtr_objc_msgSend (kls.Handle, Selector.GetHandle (Selector.Alloc));
return new NSObject (h, true);
}

public void Init ()
{
if (handle == IntPtr.Zero)
throw new Exception ("you have not allocated the native object");

handle = Messaging.IntPtr_objc_msgSend (handle, Selector.GetHandle ("init"));
}

public static void InvokeInBackground (Action action)
{
// using the parameterized Thread.Start to avoid capturing
// the 'action' parameter (it'll needlessly create an extra
// object).
new System.Threading.Thread ((v) =>
Copy link
Contributor

@Therzok Therzok Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole create a thread for one callback is worrying. This API might end up in mis-use more often than correct use.

Suggested change
new System.Threading.Thread ((v) =>
new System.Threading.Thread (static (v) =>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a somewhat useless and non-optimal API nowadays, but I think we should have the same API on all platforms.

Then we can deal with any problems with the API at the same time for all platforms (separately).

{
((Action) v) ();
})
{
IsBackground = true,
}.Start (action);
}
#endif // !COREBUILD
}

Expand Down