This lets you do nested imports inside your implementation. Something like this:
classConnectionInfo:NSManagedObject,ImportableObject{// ...funcdidInsertFromImportSource(source:[NSString:AnyObject],inTransactiontransaction:BaseDataTransaction)throws{self.earlierUrl=source["earlierUrl"]as?Stringself.laterUrl=source["laterUrl"]as?String// Here's how you can set relationships:self.connections=transaction.importObjects(Into(Connection),sourceArray:source["connections"]as?[[NSString:AnyObject]])// one-to-manyself.departure=transaction.importObject(Into(Station),source:source["departure"]as?[NSString:AnyObject])// one-to-one//... etc}
@JohnEstropia commented on GitHub (Jan 14, 2016):
@brosnic Sure it should work. If you check methods for the `ImportableObject` and `ImportableUniqueObject` protocols, the transaction is always passed:
``` swift
func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws
```
This lets you do nested imports inside your implementation. Something like this:
``` swift
class ConnectionInfo: NSManagedObject, ImportableObject {
// ...
func didInsertFromImportSource(source: [NSString: AnyObject], inTransaction transaction: BaseDataTransaction) throws {
self.earlierUrl = source["earlierUrl"] as? String
self.laterUrl = source["laterUrl"] as? String
// Here's how you can set relationships:
self.connections = transaction.importObjects(Into(Connection), sourceArray: source["connections"] as? [[NSString: AnyObject]]) // one-to-many
self.departure = transaction.importObject(Into(Station), source: source["departure"] as? [NSString: AnyObject]) // one-to-one
//... etc
}
```
I want to set relationship but I don't know how to deal with NSSet and importUniqueObjects
Thanks in advance,
@ro22e0 commented on GitHub (Mar 21, 2017):
Hello,
I want to set relationship but I don't know how to deal with NSSet and importUniqueObjects
Thanks in advance,
@ro22e0 Hi, I'm not sure which one is your use-case, but here are examples.
CoreStore.beginAsynchronous{(transaction)in// ...do{letfriends:[Person]=transaction.importUniqueObjects(Into(Person.self),sourceArray:json// Array of your ImportSource)// Case 1: If you need to reset the whole NSSetperson.friends=NSSet(array:friends)// Case 2: If you need to append// Method 1: Use KVCperson.mutableSetValue(forKey:#keyPath(Person.friends)).addObjects(from:friends)// Method 2: Copy and mutatevarfriendsCopy=Array(person.friends)friendsCopy.append(contentsOf:friends)person.friends=NSSet(array:friendsCopy)}catch{// handle error}transaction.commit{(result)in// ...}}
@JohnEstropia commented on GitHub (Mar 22, 2017):
@ro22e0 Hi, I'm not sure which one is your use-case, but here are examples.
```swift
CoreStore.beginAsynchronous { (transaction) in
// ...
do {
let friends: [Person] = transaction.importUniqueObjects(
Into(Person.self),
sourceArray: json // Array of your ImportSource
)
// Case 1: If you need to reset the whole NSSet
person.friends = NSSet(array: friends)
// Case 2: If you need to append
// Method 1: Use KVC
person
.mutableSetValue(forKey: #keyPath(Person.friends))
.addObjects(from: friends)
// Method 2: Copy and mutate
var friendsCopy = Array(person.friends)
friendsCopy.append(contentsOf: friends)
person.friends = NSSet(array: friendsCopy)
}
catch {
// handle error
}
transaction.commit { (result) in
// ...
}
}
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @brosnic on GitHub (Jan 14, 2016).
What's the suggested way to import a complex JSON?
Example:
JSON from Webservice:
CoreData Entities:
ConnectionInfo:
attributes:
relationships:
Connection:
attributes:
relationships:
and so on.
Is there a way to still use transaction.importObjects(Into(ConnectionInfo), source: jsonDictionary)?
@JohnEstropia commented on GitHub (Jan 14, 2016):
@brosnic Sure it should work. If you check methods for the
ImportableObjectandImportableUniqueObjectprotocols, the transaction is always passed:This lets you do nested imports inside your implementation. Something like this:
@brosnic commented on GitHub (Jan 14, 2016):
Thank you!
@ro22e0 commented on GitHub (Mar 21, 2017):
Hello,
I want to set relationship but I don't know how to deal with NSSet and importUniqueObjects
Thanks in advance,
@JohnEstropia commented on GitHub (Mar 22, 2017):
@ro22e0 Hi, I'm not sure which one is your use-case, but here are examples.
@ro22e0 commented on GitHub (Mar 23, 2017):
Thanks a lot !