-
-
Save spouliot/2e826ab2a86783cbd9a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using MonoTouch.ObjCRuntime; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
namespace NSPredicateProblem | |
{ | |
public partial class NSPredicateProblemViewController : UIViewController | |
{ | |
static bool UserInterfaceIdiomIsPhone { | |
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; } | |
} | |
public NSPredicateProblemViewController () | |
: base (UserInterfaceIdiomIsPhone ? "NSPredicateProblemViewController_iPhone" : "NSPredicateProblemViewController_iPad", null) | |
{ | |
} | |
public override void DidReceiveMemoryWarning () | |
{ | |
// Releases the view if it doesn't have a superview. | |
base.DidReceiveMemoryWarning (); | |
// Release any cached data, images, etc that aren't in use. | |
} | |
UILabel counter; | |
UILabel failedAttemptsLabel; | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
View.AutosizesSubviews = true; | |
// Perform any additional setup after loading the view, typically from a nib. | |
counter = new UILabel(new RectangleF(0,20,320,50)) { | |
Lines = 0 | |
}; | |
this.View.Add(counter); | |
failedAttemptsLabel = new UILabel(new RectangleF(0,70,320,250)){ | |
Lines = 0, | |
}; | |
this.View.Add(failedAttemptsLabel); | |
} | |
public override void ViewDidAppear (bool animated) | |
{ | |
base.ViewDidAppear (animated); | |
InvokeInBackground (() => { | |
int failedAttemptsNonSingleton = 0; | |
int failedAttemptsDirect = 0; | |
int failedAttemptsMethod = 0; | |
int failedAttemptsMethod2 = 0; | |
int failedAttemptsNull = 0; | |
for (long i = 0; i < long.MaxValue; i++) { | |
System.GC.Collect(); | |
// monotouch is missing support for these two bindings | |
// + (NSPredicate *)predicateWithFormat:(NSString *)format,, ... | |
// + (NSPredicate *)predicateWithFormat:(NSString *)format arguments:(va_list)argList | |
// Should be allowed to pass null to predicateWithFormat | |
// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html | |
NSPredicate predicate = null; | |
try { | |
// fails always | |
// this fails because binding for NSPredicate.FromFormat calls predicateWithFormat:argumentArray: | |
//NSPredicate.FromFormat("TRUEPREDICATE",null); | |
// passes always | |
// IntPtr_IntPtr version of needed binding | |
// NSPredicate predicate = NSPredicateExtensions.FromFormat("a == %@",new NSString("b")); | |
predicate = NSPredicate.FromFormat("a == %@", (NSObject) null); | |
// WORKS | |
// IntPtr version of needed binding | |
//NSPredicate predicate = NSPredicateExtensions.FromFormat("TRUEPREDICATE"); | |
} | |
catch (Exception e) { | |
failedAttemptsNull++; | |
} | |
// Passing Predicate Method - fails | |
// passing empty NSObject[] array to NSPredicate.FromFormat fails when predicate is passed to | |
// NSCompoundPredicate. something is looks wrong with retain count | |
try { | |
predicate = NSPredicate.FromFormat ("TRUEPREDICATE"); | |
// note: predicate.retainCount is -1 | |
predicate = AppendDeletedPredicate (predicate); | |
} | |
catch (Exception e) { | |
failedAttemptsMethod++; | |
} | |
// unrolled NSCompoundPredicate, same as above - doesn't fail | |
try { | |
NSPredicate[] predicates = new NSPredicate[] | |
{ | |
NSPredicate.FromFormat ("FALSEPREDICATE"), | |
NSPredicate.FromFormat("(markedDeleted == NO)") | |
}; | |
predicate = new NSCompoundPredicate(NSCompoundPredicateType.And, predicates); | |
} | |
catch { | |
failedAttemptsDirect++; | |
} | |
// Passing Predicate Method using correct binding - fails far less but still returns an eroor | |
// passing empty NSObject[] array to NSPredicate.FromFormat fails when predicate is passed to | |
// NSCompoundPredicate. something is looks wrong with retain count | |
try { | |
predicate = NSPredicate.FromFormat("TRUEPREDICATE"); | |
predicate = AppendDeletedPredicate2 (predicate); | |
} | |
catch (Exception e) { | |
failedAttemptsMethod2++; | |
} | |
BeginInvokeOnMainThread (() => { | |
counter.Text = String.Format ("iteration i:{0} NSPredicate.FromFormat", i); | |
failedAttemptsLabel.Text = String.Format ("Passing Null to Predicate failed:{0}\rNSCompoundPredicate from method failed:{1}\rNSCompoundPredicate unrolled failed:{2} NSCompoundPredicate w/ correct binding from method failed:{3}", | |
failedAttemptsNull, failedAttemptsMethod, failedAttemptsDirect, failedAttemptsMethod2); | |
}); | |
} | |
}); | |
} | |
public static NSPredicate AppendDeletedPredicate(NSPredicate predicate) | |
{ | |
NSPredicate[] predicates = new NSPredicate[] | |
{ | |
predicate, | |
NSPredicate.FromFormat("(markedDeleted == NO)") | |
}; | |
return new NSCompoundPredicate(NSCompoundPredicateType.And, predicates); | |
} | |
public static NSPredicate AppendDeletedPredicate2(NSPredicate predicate) | |
{ | |
NSPredicate[] predicates = new NSPredicate[] | |
{ | |
predicate, | |
NSPredicate.FromFormat("(markedDeleted == %@)", NSNull.Null) | |
}; | |
return new NSCompoundPredicate(NSCompoundPredicateType.And, predicates); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment