-
-
Save futureshocked/436145 to your computer and use it in GitHub Desktop.
This is an example on how to use MacRuby to read/write CoreData records. Tested with MacRuby 0.6 on Mac OS X 10.6.3.
This file contains hidden or 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
$ /Developer/usr/bin/momc simpleTextMessageModel.xcdatamodel simpleTextMessageModel3.mom |
This file contains hidden or 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
framework 'cocoa' | |
def fetchData (manObjCon, entity) | |
fetch = NSFetchRequest.alloc.init | |
fetch.setEntity(NSEntityDescription.entityForName(entity, inManagedObjectContext: manObjCon)) | |
fetch.setFetchLimit(25) | |
manObjCon.executeFetchRequest(fetch, error: nil) | |
end | |
def storeData(manObjCon, entity, message, recipient1, recipient2, msgType) | |
newMessage = NSEntityDescription.insertNewObjectForEntityForName(entity, inManagedObjectContext: manObjCon) | |
newMessage.textMessage = message | |
newRecipient = NSEntityDescription.insertNewObjectForEntityForName('toRecipient', inManagedObjectContext: manObjCon) | |
newRecipient.personName = recipient1 | |
newRecipient2 = NSEntityDescription.insertNewObjectForEntityForName('toRecipient', inManagedObjectContext: manObjCon) | |
newRecipient2.personName = recipient2 | |
newType = NSEntityDescription.insertNewObjectForEntityForName('Type', inManagedObjectContext: manObjCon) | |
newType.messageType = msgType | |
newMessage.messageToType = newType | |
set1 = NSSet.alloc.initWithObjects(newRecipient, newRecipient2, nil) #Storing two instances of recipient to show how to store records in a 1-many relationship | |
newMessage.messageTo = set1 | |
manObjCon.save(nil) #Can pass an error object | |
end | |
def printData(manObjCon, entity) | |
results = fetchData (manObjCon, entity) | |
puts "Total records: #{results.length}" #Returns the number of messages available | |
puts "Results class: #{results.class}" #Returns "Array" | |
results.each do |result| | |
puts "----------------------------" | |
puts "Message: #{result.textMessage}" #Returns the message text | |
recipients = result.messageTo | |
puts "recipients records: #{recipients.allObjects.count}" #Returns the number of recipients to this message | |
puts "recipients type: #{recipients.allObjects.class}" #Returns "Array" | |
msgType = result.messageToType.messageType | |
puts "Message type: #{msgType}" #Returns the class of the message | |
puts "Recipients array: #{recipients.allObjects.to_s}" | |
recipients.allObjects.each do |recipient| | |
puts "Recipient class: #{recipient.class}" #Returns "NSManagedObject_toRecipient_" | |
puts "Recipient: #{recipient.personName}" #Return the recipient name | |
end | |
end | |
end | |
def createManagedObjectContext | |
modelURL = NSURL.fileURLWithPath('simpleTextMessageModel3.mom') | |
storeURL = NSURL.fileURLWithPath('codeDataTest3.sqlite') | |
mom = NSManagedObjectModel.alloc.initWithContentsOfURL(modelURL) | |
psc = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(mom) | |
ps = psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: nil) | |
moc = NSManagedObjectContext.alloc.init | |
moc.setPersistentStoreCoordinator(psc) | |
end | |
puts "Starting" | |
moc = createManagedObjectContext | |
puts "Store something" | |
storeData(moc, "message", "#{Time.now} - this is the message", "Dr Peter", "Dr John", "A simple message") | |
puts "Print the messages" | |
printData(moc, 'message') |
This file contains hidden or 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
#import <CoreData/CoreData.h> | |
@interface message : NSManagedObject | |
{ | |
} | |
@property (nonatomic, retain) NSString * textMessage; | |
@property (nonatomic, retain) NSSet* messageTo; | |
@property (nonatomic, retain) NSManagedObject * messageToType; | |
@end | |
@interface message (CoreDataGeneratedAccessors) | |
- (void)addMessageToObject:(NSManagedObject *)value; | |
- (void)removeMessageToObject:(NSManagedObject *)value; | |
- (void)addMessageTo:(NSSet *)value; | |
- (void)removeMessageTo:(NSSet *)value; | |
@end |
This file contains hidden or 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
#import <CoreData/CoreData.h> | |
@class message; | |
@interface toRecipient : NSManagedObject | |
{ | |
} | |
@property (nonatomic, retain) NSString * personName; | |
@property (nonatomic, retain) message * messageReceived; | |
@end |
This file contains hidden or 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
#import "toRecipient.h" | |
#import "message.h" | |
@implementation toRecipient | |
@dynamic personName; | |
@dynamic messageReceived; | |
@end |
This file contains hidden or 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
#import <CoreData/CoreData.h> | |
@class message; | |
@interface Type : NSManagedObject | |
{ | |
} | |
@property (nonatomic, retain) NSString * messageType; | |
@property (nonatomic, retain) NSSet* typeToMessages; | |
@end | |
@interface Type (CoreDataGeneratedAccessors) | |
- (void)addTypeToMessagesObject:(message *)value; | |
- (void)removeTypeToMessagesObject:(message *)value; | |
- (void)addTypeToMessages:(NSSet *)value; | |
- (void)removeTypeToMessages:(NSSet *)value; | |
@end |
This file contains hidden or 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
#import "Type.h" | |
#import "message.h" | |
@implementation Type | |
@dynamic messageType; | |
@dynamic typeToMessages; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment