Saturday, March 29, 2014

Managed Object ID


  • Get the string URL representation from a ManagedObjectID
1
2
  NSURL *urlID = [objectID URIRepresentation];
  NSString *strUrlID = [urlID absoluteString];

  • Get the ManagedObjectID from a URL string
1
2
  NSURL *urlID = [[NSURL alloc] initWithString:strUrlID];
  NSManagedObjectID *objectID = [[managedObjectContext persistentStoreCoordinator] managedObjectIDForURIRepresentation:urlID];

  • Get the ManagedObject by a ManagedObjectID
1
2
  NSError *error = nil;
  NSManagedObject *managedObject = [moc existingObjectWithID:objectID error:&error];

  • Check if a ManagedObject is new
1
  BOOL isNew = managedObject.objectID.isTemporaryID;

  • Get the permanent object IDs for the objects in a child context.
1
2
3
4
5
6
7
8
  NSError *error = nil;
  [childManagedObjectContext obtainPermanentIDsForObjects:childManagedObjectContext.insertedObjects.allObjects error:&error]);

  // . . . Check for an error . . .

  [childManagedObjectContext save:&error];

  // . . . Check for an error . . .


Refs
http://stackoverflow.com/questions/3519334/how-to-uniquely-identify-nsmangedobject-with-string
http://stackoverflow.com/questions/10811280/how-to-get-the-objectid-from-url
http://stackoverflow.com/questions/3013288/how-to-use-managedobjectid-the-right-way
http://iphonedevelopment.blogspot.com.au/2009/07/core-data-determining-if-managed-object.html
http://stackoverflow.com/questions/11990279/core-data-do-child-contexts-ever-get-permanent-objectids-for-newly-inserted-obj/11996957#11996957

Thursday, March 27, 2014

Compare NSDate instances


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  NSComparisonResult result = [date1 compare:date2];  
  if (result == NSOrderedAscending)
  {
    // date1 < date2
  }
  else if (result == NSOrderedDescending)
  {
    // date1 > date2
  }
  else
  {
    // date1 == date2
  }

or

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  if ([date1 timeIntervalSinceReferenceDate] < [date2 timeIntervalSinceReferenceDate])
  {
    // date1 < date2
  }
  else if ([date1 timeIntervalSinceReferenceDate] > [date2 timeIntervalSinceReferenceDate])
  {
    // date1 > date2
  }
  else
  {
    // date1 == date2
  }

Refs
http://stackoverflow.com/questions/5965044/how-to-compare-two-nsdates-which-is-more-recent

lldb Error - no known method '-class'; cast the message send to the method's return type

p [[navigationController topViewController] class] 

returns

error: no known method '-class'; cast the message send to the method's return type 

To fix it, cast it first:- 

p [(NSObject *) [navigationController topViewController] class]

Refs
http://stackoverflow.com/questions/14007942/lldb-error-property-not-found-on-object-of-type