Realm with ObjectMapper - swift

I load a list of objects from my server and save them to Realm using ObjectMapper. Each object contains an url defining where to load the image for the object. I load the image and save the imagedata in the realm object so that I don't need to reload it every time. But unfortunately the image data is lost if I reload the data.
I use a primary key and my thought was that when the JSON. I fear that ObjectMapper doesn't update existing objects in Realm but overwrites them. So the imagedata is nil and must be refetched from server. Is there something I can do do prevent this?
Here is my simplified ObjectMapping-File:
import Foundation
import ObjectMapper
import RealmSwift
class OverviewItem: PersistentObject {
override var hashValue : Int {
get {
return self.overviewID.hashValue
}
}
dynamic var overviewID: Int = 0
dynamic var titleDe: String = ""
dynamic var imageUrl: String = ""
dynamic var imageData: NSData?
required convenience init?(_ map: Map) {
self.init()
}
//computed properties
dynamic var image: UIImage? {
get {
return self.imageData == nil ? nil : UIImage(data: self.imageData!)
}
set(newImage){
if let newImage = newImage, data = UIImagePNGRepresentation(newImage){
self.imageData = data
}
else{
self.imageData = nil
}
}
}
//image is a computed property and should be ignored by realm
override class func ignoredProperties() -> [String] {
return ["image"]
}
override func mapping(map: Map) {
overviewID <- map["infoid"]
titleDe <- map["titleDe"]
imageUrl <- map["imageurl"]
}
override class func primaryKey() -> String {
return "overviewID"
}
}
And here how I fetch the image and update the object:
func fetchImage(item: OverviewItem, successHandler: UIImage? ->(), errorHandler: (ErrorType?) -> ()){
AlamofireManager.Configured
.request(.GET, item.imageUrl)
.responseData({ (response: Response<NSData, NSError>) in
if let error = response.result.error{
logger.error("Error: \(error.localizedDescription)")
errorHandler(error)
return
}
if let imageData = response.result.value{
successHandler(UIImage(data: imageData))
let overviewID = item.overviewID
let queue = NSOperationQueue()
let blockOperation = NSBlockOperation {
let writeRealm = try! Realm()
do{
if let itemForUpdate = writeRealm.objects(OverviewItem).filter("overviewID = \(overviewID)").first{
try writeRealm.write{
itemForUpdate.imageData = imageData
writeRealm.add(itemForUpdate, update: true)
}
}
}
catch let err as NSError {
logger.error("Error with realm: " + err.localizedDescription)
}
}
queue.addOperation(blockOperation)
}
})
}

You would need to pull the existing image data by primary key before you add / upsert your object to the Realm. You could do that e.g. in a method, which abstracts ObjectMapper's mapping, but still allows providing a Realm instance.
But in general, I wouldn't recommend to store images in the Realm itself for that purpose. There are some really good image caching frameworks out there, which allow you to cache images on disk and in memory. These allow you in addition to organize the cache by size, they assist you with fast decompression and they allow you to manage expiry times.

Related

Any clean way for batch inserting coredata objects with relationships?

I have been observing high CPU times in background threads while inserting the coredata objects, and from analyser i could find that it's coming majorly because of some relationships i was creating one by one, and those could be in thousands.
So i thought if i could create them with batch insert. I can do that easily for objects using without relationships using NSBatchInsertRequest, but with relationships, I can't seem to find any clean way.Without relationships, i can easily create dictionaries and insert using the above request.
​
With relationships, i also tried using the object handler method of NSBatchInsertRequest, but even that is giving me an exception
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'run' between objects in different contexts
This is how i am trying to make sure that the trackpoint getting added is using the run object from the same context as the one in which its being created
func addTrackPoints(run: RunModel, objectId: NSManagedObjectID) async throws {
let locations:[CLLocation] = run.getLocations()
let count = run.getLocations().count
var index = 0
let batchInsert = NSBatchInsertRequest(entity: TrackPoint.entity()) { (managedObject: NSManagedObject) -> Bool in
guard index < count else { return true }
if let trackPoint = managedObject as? TrackPoint {
let data = locations[index]
guard let run = try? StorageService.shared.getBackgroundContext().object(with: objectId) as? Run else {
fatalError("failed to get run object")
}
trackPoint.run = run
}
index += 1
return false
}
try await StorageService.shared.batchInsert(entity: TrackPoint.entity(), batchInsertRequest: batchInsert, context: StorageService.shared.getBackgroundContext())
}
I also tried it without accessing the object from same context but instead tried directly using the Run object that i had created. It didn't crash, but it still didn't create the relationship.Also it forced me to remove the concurrencydebug run argument.
func addTrackPoints(run: RunModel, object: Run) async throws {
let locations = run.getLocations()
let count = run.getLocations().count
var index = 0
let batchInsert = NSBatchInsertRequest(entity: TrackPoint.entity()) { (managedObject: NSManagedObject) -> Bool in
guard index < count else { return true }
if let trackPoint = managedObject as? TrackPoint {
let data:CLLocation = locations[index]
trackPoint.run = object
}
index += 1
return false
}
try await StorageService.shared.batchInsert(entity: TrackPoint.entity(), batchInsertRequest: batchInsert, context: StorageService.shared.getBackgroundContext()) }
StorageService
public func batchInsert(entity: NSEntityDescription, batchInsertRequest: NSBatchInsertRequest, context: NSManagedObjectContext? = nil) async throws {
var taskContext:NSManagedObjectContext? = context
if(taskContext == nil) {
taskContext = StorageService.shared.newTaskContext()
// Add name and author to identify source of persistent history changes.
taskContext?.name = "importContext"
taskContext?.transactionAuthor = "import\(entity.name ?? "entity")"
}
/// - Tag: performAndWait
try await taskContext?.perform {
// Execute the batch insert.
do{
let fetchResult = try taskContext?.execute(batchInsertRequest)
if let batchInsertResult = fetchResult as? NSBatchInsertResult,
let success = batchInsertResult.result as? Bool, success {
return
}
} catch {
self.logger.error("Failed to execute batch insert request. \(error)")
}
throw SSError.batchInsertError
}
logger.info("Successfully inserted data for \(entity.name ?? "entity")")
}
Any help would be deeply appreciated :-)
How app works, I send request to server, get some results and want data to be saved in core data for further usage to send request to server only when needed. so next time I will query data from database.
Here is sample:
I always save data in background context, which is configured like this:
func getBgContext() -> NSManagedObjectContext {
let bgContext = self.persistenceController.container.newBackgroundContext()
bgContext.automaticallyMergesChangesFromParent = true
bgContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
return bgContext
}
Next I construct my data models like this so decoder will handle entity creation and data parsing + insertion in dbContext:
public class SomeDataModel: NSManagedObject, Codable {
var entityName: String = "SomeDataModel"
enum CodingKeys: String, CodingKey {
case id = "id"
case someData = "someData"
}
public required convenience init(from decoder: Decoder) throws {
guard
let context = decoder.userInfo[CodingUserInfoKey.managedObjectContext] as? NSManagedObjectContext,
let entity = NSEntityDescription.entity(forEntityName: "SomeDataModel", in: context)
else {
throw DecoderConfigurationError.missingManagedObjectContext
}
self.init(entity: entity, insertInto: context)
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decode(Int32.self, forKey: .id)
someData = try values.decodeIfPresent(String.self, forKey: .someData)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(someData, forKey: .someData)
}
func toExternalModel() -> SomeExternalUsableModel {
return SomeExternalUsableModel(id: id, someData: someData)
}
}
extension SomeDataModel {
#nonobjc public class func fetchRequest() -> NSFetchRequest<SomeDataModel> {
return NSFetchRequest<SomeDataModel>(entityName: "SomeDataModel")
}
#NSManaged public var someData: String?
#NSManaged public var id: Int32
}
extension SomeDataModel: Identifiable {
}
to pass dbcontext to decoder I do next:
extension CodingUserInfoKey {
static let managedObjectContext = CodingUserInfoKey(rawValue: "managedObjectContext")!
}
dbContext - create background context somewhere in API helper class, and use this context for all the parsings below.
and next I do parsing with decoder when respond from server comes:
let model = try self.dbContext.performAndWait {
let jsonDecoder = JSONDecoder()
let jsonEncoder = JSONEncoder()
// pass context to decoder/encoder
jsonDecoder.userInfo[CodingUserInfoKey.managedObjectContext] = self.dbContext
jsonEncoder.userInfo[CodingUserInfoKey.managedObjectContext] = self.dbContext
// parse model, used generic for reuse for other models
let model = try jsonDecoder.decode(T.self, from: result.data)
// after this line - all the data is parsed from response from server, and saved to dbContext, and contained in model as well
if self.dbContext.hasChanges {
do {
try self.dbContext.save()
self.dbContext.refreshAllObjects() // refresh context objects to ELIMINATE all outdated db objects in memory (specially when you will have relations, they could remain in memory until updated)
} catch let error {
// process error
}
}
return model
}
// do with saved and ready to use data in models whatever needed:
return model
and extensions used for performAndWait
extension NSManagedObjectContext {
func performAndWait<T>(_ block: () throws -> T) throws -> T? {
var result: Result<T, Error>?
performAndWait {
result = Result { try block() }
}
return try result?.get()
}
func performAndWait<T>(_ block: () -> T) -> T? {
var result: T?
performAndWait {
result = block()
}
return result
}
}

How To Save Only One Instance Of Class In Realm

So instead of using user defualts I want to persist some settings using Realm.
I've created a class for the settings
import Foundation
import RealmSwift
class NutritionSettings: Object {
#objc dynamic var calories: Int = 0
#objc dynamic var proteins: Int = 0
#objc dynamic var carbohydrates: Int = 0
#objc dynamic var fats: Int = 0
}
But in my view controller I don't know how to save just one instance of it
I've tried
let realm = try! Realm()
let settings = NutritionSettings()
do {
try realm.write{
settings.calories = cals!
settings.carbohydrates = carbs!
settings.fats = fats!
settings.proteins = proteins!
}
} catch {
print("error saving settings")
}
Since I know doing realm.add would just add another NutritionSettings object which is not what I want. I was unable to clarify anything using the documentation. Any help would be appreciated thanks.
I faced a similar issue in my project when I tried to save a user session object. If you want to save a unique object, override the primaryKey() class method and set the unique key for it.
#objcMembers class NutritionSettings: Object {
static let uniqueKey: String = "NutritionSettings"
dynamic var uniqueKey: String = NutritionSettings.uniqueKey
dynamic var calories: Int = 0
override class func primaryKey() -> String? {
return "uniqueKey"
}
}
Then to receive the object just use the unique key.
// Saving
let settings = NutritionSettings()
settings.calories = 100
do {
let realm = try Realm()
try realm.write {
realm.add(settings, update: .modified)
}
} catch {
// Error handling
}
// Reading
var settings: NutritionSettings?
do {
let realm = try Realm()
let key = NutritionSettings.uniqueKey
settings = realm.object(ofType: NutritionSettings.self, forPrimaryKey: key)
} catch {
// Error handling
}
if let settings = settings {
// Do stuff
}
Hope it will help somebody.
If you look at the example realm provides https://realm.io/docs/swift/latest you can see that in order to only save one object you still have to do an add. Once you have added the object to the database you can fetch that object and do a write that modifies the internal properties
let realm = try! Realm()
let settings = NutritionSettings()
settings.id = 1
do {
try realm.write{
realm.add(settings)
}
} catch {
print("error saving settings")
}
Next you can fetch and modify that single instance that you saved
let realm = try! Realm()
let settings = realm.objects(NutritionSettings.self).filter("id == 1").first
do {
try realm.write{
settings.calories = cals!
settings.carbohydrates = carbs!
settings.fats = fats!
settings.proteins = proteins!
}
} catch {
print("error saving settings")
}

Swift Realm change only one object value

class User: Object {
#objc dynamic var id = ""
#objc dynamic var dateFirstStart:TimeInterval = 0
//dates
#objc dynamic var dateLastStart:TimeInterval = 0
#objc dynamic var dateLastAppClose:TimeInterval = 0
#objc dynamic var dateLastDataUpdateCheck:TimeInterval = 0
#objc dynamic var dateLastFilesUpdateCheck:TimeInterval = 0
override class func primaryKey() -> String? {
return "id"
}
}
Do I really have to create a function for each value to change? Like this:
func updateUserDateFirstStart(date:Date){
do {
let realm = try Realm()
try realm.write {
let user = getUser()
user. dateLastStart = Date().timeIntervalSince1970
}
} catch let error as NSError {
print("ERROR \(error.localizedDescription)")
}
}
What I want is something like
let user = getUser()
user.dateLastStart = Date().timeIntervalSince1970
dataManager.updateUser(user)
And in my DataManager:
func updateUser(user:User){
do {
let realm = try Realm()
try realm.write {
realm.add(user, update: true)
}
} catch let error as NSError {
print("ERROR \(error.localizedDescription)")
}
}
But if I do it as you can see in my wishtohave solution I always get an Attempting to modify object outside of a write transaction error.
I tried to create a complete new Object and use the id from the object I want to change. This works but would need even more lines of code.
You can use KVO to update one value in realm object
how to call
let user = getUser()
self.update(ofType:user, value: Date().timeIntervalSince1970 as AnyObject, key: "dateLastStart")
Helper func
func update(ofType:Object,value:AnyObject,key:String)->Bool{
do {
let realm = try Realm()
try realm.write {
ofType.setValue(value, forKeyPath: key)
}
return true
}catch let error as NSError {
fatalError(error.localizedDescription)
}
return false
}

Hold reference to downloaded DynamoDB data

I have a class holding a DynamoDB model (I cut the # of variables for brevity, but they're all Optional Strings:
import AWSCore
import AWSDynamoDB
#objcMembers class Article: AWSDynamoDBObjectModel, AWSDynamoDBModeling {
var _articleSource: String?
class func dynamoDBTableName() -> String {
return "article"
}
class func hashKeyAttribute() -> String {
return "_articleId"
}
class func rangeKeyAttribute() -> String {
return "_articleUrl"
}
override class func jsonKeyPathsByPropertyKey() -> [AnyHashable: Any] {
return [
"_articleSource" : "articles.articleSource",
]
}
}
In my View Controller, I'm downloading data from the table and storing each article in an array like this:
let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
var allArticles = [AnyObject]()
func getArticles(completed: #escaping DownloadComplete) {
let scanExpression = AWSDynamoDBScanExpression()
scanExpression.limit = 50
self.dynamoDbObjectMapper.scan(Article.self, expression: scanExpression).continueWith(block: { (task:AWSTask<AWSDynamoDBPaginatedOutput>!) -> Any? in
if let error = task.error as NSError? {
print("The request failed. Error: \(error)")
} else if let paginatedOutput = task.result {
for article in paginatedOutput.items as! [Article] {
self.allArticles.append(article)
}
}
return(self.allArticles)
})
completed()
}
When I try to work with the data that should be stored in allArticles the array is empty. However, the array holds articles when I break execution in the download block where articles are being appended. How can I hold reference to the downloaded data? My use of a completion block was my attempt.
Edit: allArticles is of type [AnyObject] because I'm attempting to store objects from 3 different classes total in the same array to make it easier to work with in a TableView
The array wasn't empty after all, I just didn't realize this was all async (duh...)
I just needed:
DispatchQueue.main.async {
self.tableView.reloadData()
}
in place of completed() in the getArticles() func

Fetch request on single Transformable attribute (Swift)

I have a Core Data entity (type) called NoteEntity. It has a managed variable called noteDocument, which is of the custom type NoteDocument (my subclass of NSDocument). I changed its auto-generated NoteEntity+Core Data Properties class so it reads
import Foundation
import CoreData
extension NoteEntity {
#NSManaged var noteDocument: NoteDocument? // changed
#NSManaged var belongsTo: NSSet?
}
so that noteDocument is of type NoteDocument instead of NSObject. The NoteDocument class does implement NSCoding, as follows:
required convenience init(coder theDecoder: NSCoder)
{
let retrievedURL = theDecoder.decodeObjectForKey("URLKey") as! NSURL
self.init(receivedURL: retrievedURL)
}
func encodeWithCoder(theCoder: NSCoder)
{
theCoder.encodeObject(fileURL, forKey: "URLKey")
}
What I want to be able to do is find the noteEntity entities in the managed context with a given noteDocument value. So I run this code (passing it a parameter theNote that corresponds to a noteDocument that I know exists in the managed context):
var request = NSFetchRequest(entityName: "NoteEntity")
let notePredicate = NSPredicate(format: "noteDocument == %#", theNote)
request.predicate = notePredicate
print("Text in the NoteEntity with the NoteDocument for "+theNote.filename+":")
do
{
let notesGathered = try context.executeFetchRequest(request) as? [NoteEntity]
for n in notesGathered!
{
print (n.noteDocument!.filename)
print (n.noteDocument!.noteText)
}
}
catch let error as NSError
{
print("Could not run fetch request. \(error), \(error.userInfo)")
}
but it returns no entries. If I comment out the predicate, I get all the NoteEntity values in the database, but with the predicate in there I get nothing. Clearly something is wrong with the search I'm trying to do in the predicate. I think it's because the value is a Transformable, but I'm not sure where to go from there. I know you can't run fetch requests on the members of Transformable arrays, but is it not possible to run fetch requests on single Transformable attributes? If it isn't, what alternatives exist?
EDIT: The NoteDocument class includes a lot more than the NSCoding. As I said, it's an NSDocument subclass. The NSCoding uses a URL as its key because that's the "primary key" for the NoteDocument class - it's what initializes the class. Here is the rest of the class, not including the NSCoding above:
import Cocoa
class NoteDocument: NSDocument, NSCoding
{
var filename: String
var noteText: String
var attributes: NSDictionary?
var dateCreated: NSDate?
var dateString: String?
init (receivedURL: NSURL)
{
self.filename = ""
self.noteText = ""
super.init()
self.fileType = "net.daringfireball.markdown"
self.fileURL = receivedURL
// Try to get attributes, most importantly date-created.
let fileManager = NSFileManager.defaultManager()
do
{
attributes = try fileManager.attributesOfItemAtPath(fileURL!.path!)
}
catch let error as NSError
{
print("The error was: "+String(error))
}
if let dateCreated = attributes?.fileCreationDate()
{
// print("dateCreated is "+String(dateCreated!))
// Format the date-created to an appropriate string.
dateString = String(dateCreated)
}
else
{
print("Did not find the attributes for "+filename)
}
if let name = self.fileURL?.lastPathComponent
{
filename = name
}
else
{
filename = "Unnamed File"
}
noteText = ""
do
{
noteText = try NSString(contentsOfURL: self.fileURL!, encoding: NSUTF8StringEncoding) as String
}
catch let error as NSError
{
print("Error trying to get note file:"+String(error))
}
}
// MARK: - Document functions
override class func autosavesInPlace() -> Bool
{
// print ("autosavesInPlace ran.")
return true
}
override func dataOfType(typeName: String) throws -> NSData
{
var outError: NSError! = NSError(domain: "Migrator", code: 0, userInfo: nil)
// Post: Document is saved to a file specified by the user.
outError = NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
if let value = self.noteText.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
// Convert noteText to an NSData object and return that.
return value
}
print("dataOfType ran.")
throw outError
}
override func readFromData(data: NSData, ofType typeName: String) throws
{
// Currently unused; came free with NSDocument.
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
}
In the code you show that the only thing you're coding is a URL. In this case it makes much more sense and provides more utility to use a plain string in the entity to store the URL and to add a transient attribute (or create a wrapper class to combine the entity and document) for the document. In this way you can use the URL in the predicate and it's easy to build the predicate from a document. Storing the document as a transformable isn't helping you in any way it seems.