I'm writing some Unit tests for my Realm model but I'm getting a problem when updating saved Realm objects. Th problem is about losing the one-to-many references after an update.
Please, see the example below:
(I'm using Quick, but it should be readable even if you don't know it)
// Create a test User.
let user = User.init()
user.account_id = 1
user.email = "foo#bar.com"
user.cards.append(Car(value: ["id": 1]))
user.cards.append(Car(value: ["id": 2]))
user.boxes.append(Box(value: ["id": 5]))
user.boxes.append(Box(value: ["id": 6]))
// Save User.
self.testRealm.write { () -> Void in
self.testRealm.add(user, update: true)
}
// Get User.
let testUser = self.testRealm.objectForPrimaryKey(User.self, key: 1)
expect(testUser).toNot(beNil())
// Update the user by creating a new one.
let newuser = User.init()
newuser.account_id = 1
newuser.email = "fooBar#bar.com"
// Update a test User.
self.testRealm.write { () -> Void in
self.testRealm.add(newuser, update: true)
}
// Get User again.
let testUpdatedUser = self.testRealm.objectForPrimaryKey(User.self, key: testUserId)
expect(testUpdatedUser?.email).to(equal("fooBar#bar.com"))
expect(testUpdatedUser?.boxes.count).to(equal(2)) <--- FAILS
expect(testUpdatedUser?.cards.count).to(equal(2)) <--- FAILS
And my Model:
import RealmSwift
class User: Object {
override static func primaryKey() -> String? {
return "account_id"
}
dynamic var email = ""
// The list of Boxes that user owns.
let boxes = List<Box>()
// The list of Cars that user owns.
let cars = List<Car>()
}
That's the expected behavior. Properties, which have nil values, are merged with the existing values if you set the optional parameter update: true. List properties may not be nil. An empty list could be also a legitimate more up-to-date value than a list of some objects.
You'd need to retrieve the object and manually merge the properties according to your will.
Related
I have two entities, User and Post. A User contains many Post, and each Post has only one User.
I am trying to batch insert more than 1000 posts.
private func newBatchInsertRequest(with posts: [PostData]) -> NSBatchInsertRequest {
var index = 0
let total = posts.count
let batchInsert = NSBatchInsertRequest(entity: Post.entity()) { (managedObject: NSManagedObject) -> Bool in
guard index < total else { return true }
if let post = managedObject as? Post {
let data = posts[index]
post.createdData = data.createdDate
post.identifier = data.identifier
post.text = data.text
}
index += 1
return false
}
PersistenceController.shared.container.performBackgroundTask { context in
try? context.execute(batchInsert)
try? context.save()
}
}
It inserts all the posts that I want to insert. However, I can not configure how to set their User.
I tried to use the following code, but it did not work.
let updateRequest = NSBatchUpdateRequest(entity: Post.entity())
updateRequest.resultType = .updatedObjectIDsResultType
updateRequest.propertiesToUpdate = ["user": user]
try? context.execute(updateRequest)
I get the following error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid relationship ((<NSRelationshipDescription: 0x2801045a0>), name item, isOptional 1, isTransient 0, entity Post, renamingIdentifier item
I can set their user one by one, but it is inefficient due to long processing time.
How to update the user property of Posts in a more efficient way?
Update
All of these posts belongs to one User which does not exist yet. I need to create it before or after executing the NSBatchInsertRequest.
User has three properties
1. createdDate: Date
2. identifier: UUID
3. name: String
My goal is to insert Post that belongs to one Use either using NSBatchInsertRequest or in a private context so that it does not block the main thread.
Here's what I would try:
Create the User before your batch operation. Yep, it's going to be on a different context — just make sure you grab its NSManagedObjectID (it's a property on NSManagedObject) and save it on a private property.
Inside your PersistenceController.shared.container.performBackgroundTask you have a reference to your private context. Use the managed object ID from before to fetch/register the same User in your private context:
let user = context.object(with: userManagedObjectID)
Then pass this user into the method that creates your batch insert request to set the user property on your Post objects.
I believe if you set up your inverse relationship up in the data model editor you don't need to populate User.posts. That should happen automatically for you.
I solved this problem in different way.
In my entity which needs to have relationship added placeholder attributed tmpID.
When creating batch inserts setting tmpID to particular string. As example parent objectID parent.objectID.
Executing batch insert.
Then fetching all object from CoreData with tmpID.
Then going throw everyone and setting:
Relationship
tmpID to nil, the do not waist a memory.
Saving managed object context.
To merge I am using function:
/// Executes the given `NSBatchDeleteRequest` and directly merges the changes to bring the given managed object context up to date.
///
/// - Parameter batchDeleteRequest: The `NSBatchDeleteRequest` to execute.
/// - Throws: An error if anything went wrong executing the batch deletion.
public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws {
batchDeleteRequest.resultType = .resultTypeObjectIDs
let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult
let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self])
}
// Use case:
do {
try container.viewContext.executeAndMergeChanges(using: deleteReqest)
try container.viewContext.save()
} catch {
Logger.app.e("Can not destroy/save clean table: \(name) -> \(error)")
}
I'm currently having trouble querying data that I stored in an Inverse relationship. I have the following code
class Form: Object {
dynamic var id: String = NSUUID().uuidString
...
var answers = List<FormAnswer>()
override static func primaryKey() -> String? {
return "id"
}
...
}
and
class FormAnswer: Object {
dynamic var key = ""
dynamic var answer = ""
let form = LinkingObjects(fromType: Form.self, property: "answers")
override static func primaryKey() -> String? {
return "key"
}
}
When I create a FormAnswer object I do the following:
try! realm.write {
let answer = FormAnswer(value: ["key": key, "answer": answer, "form" : parentForm!]) // parentForm is of type "form"
realm.add(answer, update: true)
}
And when I try to query it, I get nothing!
let previousValue = realm.objects(FormAnswer.self).filter("key == %# AND ANY form.id == %#", key, parentForm!.id).first?.answer
I've checked the realm file with Realm Browser, and there's an entry for FormAnswer. But there are only 2 fields (key and answer) and there doesn't appear to be a link to my Form object.
Does anyone have any ideas on how I can fix this?
Thanks
LinkingObjects is a computed property and cannot be mutated directly. Instead you modify its values by changing the other side of the relationship.
Try:
try! realm.write {
parentForm.answers.add(FormAnswer(value: ["key": key, "answer": answer]))
}
This adds the new answer directly to the Form's answer list, and will result in the answer's form property containing parentForm.
Based on the following code I would like to be able to create a new ItemList from an existing one. In other words I have an ItemList called First List and I want to create a new ItemList, call it Second List and fill it with the Items from First List.
The way I have it right now is that it creates the Second List as expected, the Items from the First List show in Second List but what doesn't work is when I want to delete only the Items from First List, it deletes Items from both lists. I guess I'm not truly copying the items.
So the question is, how can I copy Items from First List to Second List?
Object Models:
class ItemList: Object {
dynamic var listName = ""
dynamic var createdAt = NSDate()
let items = List<Item>()
}
class Item:Object{
dynamic var productName:String = ""
dynamic var createdAt = NSDate()
}
Code to create Second List from First List
This Works fine, it creates Second List and adds the items from First List but I don't think I'm making copies just showing them in Second List.
let newList = ItemList()
newList.listName = "Second List"
if let selectedList = realm.objects(ItemList.self).filter("listName = %#", "First List").first{
let itemsFromFirstList = selectedList.items
newList.items.append(objectsIn:itemsFromFirstList)
}
try! realm.write {
realm.add(newList)
}
This code is supposed to delete only the items from First List
This actually deletes items from both First List and Second List
let listToDelete = realm.objects(ItemList.self).filter("listName = %#", "First List").first
try! realm.write {
for item in (listToDelete?.items)! {
realm.delete(realm.objects(Item.self).filter("productName = %#", item.productName).first!)
}
}
What you want to do is use:
for record in postsDB.objects(PostModel.self) {
if !combinedDB.objects(PostModel.self).filter("postId == \(record.parentId)").isEmpty {
combinedDB.create(PostModel.self, value: record, update: false)
}
}
The create method is inherited from Object. It tells the target to create a new object. Use true if you want it to look to see if there is already a record there, and update it if there is.
PostModel is the Object type, record is what you want copied.
Edit: I added the if statement to provide more context. You didn't show your class definitions, so I was guessing. This is a working example. I ask for a set of records from DatabaseA and copy it to DatabaseB (postsDB to combinedDB).
So if the type of the object you're trying to insert is a List, I'd recommend you define a subclass of Object, and have at least the list you need as a property.
class TagList: Object {
dynamic var tag = ""
var list = List<PostModel>()
override class func primaryKey() -> String? {
return "tag"
}
}
Full working example illustrating: creating new objects, copying all objects to a second list, deleting from second list after copying, adding to first list (which didn't get anything deleted from it.
import Foundation
import RealmSwift
class Letter: Object {
dynamic var letter = "a"
}
class Letters: Object {
var letterList = List<Letter>()
}
class ListExample {
let listRealmStore = try! Realm() // swiftlint:disable:this force_try
func testThis() {
print(Realm.Configuration.defaultConfiguration.fileURL!)
listRealmStore.beginWrite()
addSingleItems() // add 3 objects to the DB
let firstList = Letters()
let allObjects = listRealmStore.objects(Letter.self)
for item in allObjects {
firstList.letterList.append(item)
}
let secondList = Letters()
let itemsToCopy = firstList.letterList
for item in itemsToCopy {
let obj = listRealmStore.create(Letter.self)
obj.letter = item.letter
secondList.letterList.append(obj)
}
let third = Letter()
third.letter = "Z"
listRealmStore.add(third)
firstList.letterList.append(third)
secondList.letterList.removeLast()
do {
try listRealmStore.commitWrite()
} catch let error {
print("couldn't commit db writes: \(error.localizedDescription)")
}
print("list one:\n\(firstList)")
print("list two:\n\(secondList)")
}
func addSingleItems() {
for letter in ["a", "b", "c"] {
let objectToInsert = Letter()
objectToInsert.letter = letter
listRealmStore.add(objectToInsert)
}
}
}
Results in:
list one:
Letters {
letterList = List<Letter> (
[0] Letter {
letter = a;
},
[1] Letter {
letter = b;
},
[2] Letter {
letter = c;
},
[3] Letter {
letter = Z;
}
);
}
list two:
Letters {
letterList = List<Letter> (
[0] Letter {
letter = a;
},
[1] Letter {
letter = b;
}
);
}
Are you really trying to create copies of your items, or do you just want to be able to remove them from lists independently?
When you do:
newList.items.append(objectsIn: itemsFromFirstList)
you end up with the same objects being in both lists. List<T> just stores references to objects that live within the Realm. Appending an object to a List just references the existing object, it doesn't copy the object.
When you call Realm.delete(_:) you remove that object entirely from the Realm, not just from a single list that it is a member of. To remove an object from a List, you should instead use List.remove(objectAtIndex:).
One part the solution you are looking for could be like this, make copy objects in the list, or you can just use this idea to clone whole list it self:
Previously answered here
As of now, Dec 2020, there is not proper solution of this issue. We have many workarounds though.
Here is the one I have been using, and one with less limitations in my opinion.
Make your Realm Model Object classes conform to codable
class Dog: Object, Codable{
#objc dynamic var breed:String = "JustAnyDog"
}
Create this helper class
class RealmHelper {
//Used to expose generic
static func DetachedCopy<T:Codable>(of object:T) -> T?{
do{
let json = try JSONEncoder().encode(object)
return try JSONDecoder().decode(T.self, from: json)
}
catch let error{
print(error)
return nil
}
}
}
Call this method whenever you need detached / true deep copy of your Realm Object, like this:
//Suppose your Realm managed object: let dog:Dog = RealmDBService.shared.getFirstDog()
guard let detachedDog = RealmHelper.DetachedCopy(of: dog) else{
print("Could not detach Note")
return
}
//Change/mutate object properties as you want
detachedDog.breed = "rottweiler"
As you can see we are piggy backing on Swift's JSONEncoder and JSONDecoder, using power of Codable, making true deep copy no matter how many nested objects are there under our realm object. Just make sure all your Realm Model Classes conform to Codable.
Though its NOT an ideal solution, but its one of the most effective workaround.
I've been learning iOS development for the past three weeks, I'm currently following a course on Udemy so far so good.
However I'm following one of the lectures whereby we build an Instagram Clone.
The instructor is using three arrays which are as follows:
var usernames = [""] // Stores all usernames
var userIds = [""] // Stores all Id's of the given usernames
var isFollowing = [false] // Stores where or not you're following that user
To me trying to keep track of what userId goes with what username using two arrays is basically an accident waiting to happen so I decided to set off and find a more feasible approach. I reverted back to my .Net days and decided to create a list so I went and created a class as follows:
class Users{
var Username : NSString = ""
var UserId : NSString = ""
var Following : Bool = false
}
Now inside my ViewController I make a call to Parse which returns me a list of users and I'm basically trying to loop through the response, and add them to the list class as shown here:
var t = [Users]() // After googling the web, this seems to be the syntax for a list declaration ?
let u = Users()
for object in users{
if let o = object as? PFUser {
u.Username = o.username!
u.UserId = o.objectId!
u.Following = o.IsFollowing!
self.t.append(u)
}
}
print(self.t)
Now when I print this to the console I see the following:
ParseStarterProject_Swift.Users
As I have one user at present, however when I try to loop through T and display the username in the console it doesn't display anything.
for x in t {
print(x.Username)
}
Your basic intuition is correct, it's better to have an array of custom objects, not multiple arrays.
Regarding making it more Swifty, consider your Users type. You might want something like:
struct User {
let username: String
let userId: String
let following: Bool
}
Note,
property names should start with lowercase letter;
Users should probably be called User, as it represents a single user;
we don't generally initialize values to default values like that, but rather specify them in the initializer;
we probably use String not NSString;
if a property cannot change, you'd use let, not var;
properties begin with lower case letters;
Then you can do something like:
var t = [User]()
for object in users {
if let o = object as? PFUser {
t.append(User(username: o.username!, userId: o.objectId!, following: o.IsFollowing!)
}
}
print(t)
Clearly, with all of those ! forced unwrapping operators, you'd want to be confident that those fields were populated for all of those properties.
Using struct is nice because (a) it's a value type; (b) you get the initializer for free; and (c) you can just print them. If you really wanted User to be a reference type (a class), you'd do something like:
class User {
let username: String
let userId: String
let following: Bool
init(username: String, userId: String, following: Bool) {
self.username = username
self.userId = userId
self.following = following
}
}
And if you wanted to be able to just print them, you'd define it to conform to CustomStringConvertible:
extension User: CustomStringConvertible {
var description: String { return "<User; username = \(username); userId = \(userId); following = \(following)>" }
}
With the class, you can feel free to change that description computed property to show it in whatever format you want, but it illustrates the idea.
You are correct in considering that keeping track of what userId goes with what username using two arrays is dangerous, you in the correct direction with your approach.
First, I would just like to suggest that you use correct naming convention:
Classes should be singular (except in very specific cases).
Variable/property names should begin with lowercase.
This would mean that your user class should look like this:
class User {
var username : NSString = ""
var userId : NSString = ""
var following : Bool = false
}
I will keep your existing naming use for the next part. The main problem with your code is that the variable "u" is a object which you create only once and then modify it. You should be creating a new "Users" object for each user instead of modifying the original. If you don't do this you will just have an array with the same user multiple times. This is how your code would look now:
var t = [Users]()
for object in users {
if let o = object as? PFUser {
let u = Users()
u.Username = o.username!
u.UserId = o.objectId!
u.Following = o.IsFollowing!
self.t.append(u)
}
}
print(self.t)
Next you mention that when you print to console you see the text: ParseStarterProject_Swift.Users, that is because Swift does not automatically print a pretty text with the content of your object. In order for it to print something more detailed, your "Users" object would need to implement the CustomStringConvertible. You can see a more detailed answer about that here: how-can-i-change-the-textual-representation-displayed-for-a-type-in-swif.
Lastly, you mention that when you loop trough "t" and display the username in the console it does not display anything. This is caused by one of two things:
Because there are no users being returned from parse, so the "t" array is actually empty. Try print(t.count) to see how many objects are in the array.
Because your "Users" object declares an empty string "" as the default username and the username is not being set correctly when getting the data from the parse. Which means that it IS actually printing something, just that it is an empty string. Try defining a different default value like var username : NSString = "Undefined" to see if it prints something.
Good luck learning swift!
I've setup a REST API to realm object in iOS. However I've found an issue with creating a favorite flag in my object. I've created a favorite bool, however everytime the object is updated from the API it sets the favorite to default false again. Here I want this flag to not be updated, since the favorite only is stored locally. How can I achieve this?
class Pet: Object{
dynamic var id: Int = 1
dynamic var title: String = ""
dynamic var type: String = ""
dynamic var favorite: Bool = false
override class func primaryKey() -> String {
return "id"
}
}
CreateOrUpdate
let pet = Pet()
pet.id = 2
pet.name = "Dog"
pet.type = "German Shephard"
try! realm.write {
realm.add(pet, update: true)
}
There are two ways to solve this:
1. Use an Ignored Property:
You can tell Realm that a certain property should not be persisted. To prevent that your favorite property will be persisted by Realm you have to do this:
class Pet: Object{
dynamic var id: Int = 1
dynamic var title: String = ""
dynamic var type: String = ""
dynamic var favorite: Bool = false
override class func primaryKey() -> String {
return "id"
}
override static func ignoredProperties() -> [String] {
return ["favorite"]
}
}
or you could
2. Do a partial update
Or you could tell Realm explicitly which properties should be updated when you update your Pet object:
try! realm.write {
realm.create(Pet.self, value: ["id": 2, "name": "Dog", "type": "German Shepard"], update: true)
}
This way the favorite property will not be changed.
Conclusion
There is one big difference between the two approaches:
Ignored Property: Realm won't store the favorite property at all. It is your responsibility to keep track of them.
Partial Update: Realm will store the 'favorite' property, but it won't be updated.
I suppose that the partial updates are what you need for your purpose.
If you want to be more explicit, there is third option:
3. Retrieve the current value for the update
// Using the add/update method
let pet = Pet()
pet.id = 2
pet.name = "Dog"
pet.type = "German Shephard"
if let currentObject = realm.object(ofType: Pet.self, forPrimaryKey: 2) {
pet.favorite = currentObject.favorite
}
try! realm.write {
realm.add(pet, update: true)
}
// Using the create/update method
var favorite = false
if let currentObject = realm.object(ofType: Pet.self, forPrimaryKey: 2) {
favorite = currentObject.favorite
}
// Other properties on the pet, such as a list will remain unchanged
try! realm.write {
realm.create(Pet.self, value: ["id": 2, "name": "Dog", "type": "German Shephard", "favorite": favorite], update: true)
}
4. NSUserDefaults (or any other data store, really)
I ran into the same issue and I opted for the other, more traditional option of saving things to another data store (NSUserDefaults). In my case, I was storing the last time a user viewed an item and storing this data in NSUserDefaults felt appropriate. I did something like the following:
First, define a unique key for the object you are storing (self here is the model object being viewed and rendered):
- (NSString *)lastViewedDateKey {
// Note each item gets a unique key with <className>_<itemId> guaranteeing us uniqueness
return [NSString stringWithFormat:#"%#_%ld", self.class.className, (long)self.itemId];
}
Then, when a user views the item, set the key to:
- (void)setLastViewedToNow {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSDate date] forKey:self.lastViewedDateKey];
[userDefaults synchronize];
}
Later, I use it like this:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDate *createdOnDate = <passed in from elsewhere>;
NSDate *lastViewedDate = [userDefaults objectForKey:self.lastViewedDateKey];
if (!lastViewedDate || [createdOnDate compare:lastViewedDate] == NSOrderedDescending) {
...
}
As opposed to the above solutions:
1. There is no data persistence.
2. This creates yet another place where one has to define the properties of an object and will likely lead to errors if one forgets to update this list every time a new property gets added.
3. If you are doing any large batch updates, going back through every object is not really practical and will certainly cause pain down the road.
I hope this gives someone another option if they need it.