Skip to content

Instantly share code, notes, and snippets.

@crafterm
Forked from futureshocked/Bash
Created June 23, 2010 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crafterm/449523 to your computer and use it in GitHub Desktop.
Save crafterm/449523 to your computer and use it in GitHub Desktop.
$ /Developer/usr/bin/momc simpleTextMessageModel.xcdatamodel simpleTextMessageModel3.mom
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')
#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
#import <CoreData/CoreData.h>
@class message;
@interface toRecipient : NSManagedObject
{
}
@property (nonatomic, retain) NSString * personName;
@property (nonatomic, retain) message * messageReceived;
@end
#import "toRecipient.h"
#import "message.h"
@implementation toRecipient
@dynamic personName;
@dynamic messageReceived;
@end
#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
#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