I am developing an application with swift and parse.com, but I have problems with the model of the application.
I have an Event class that contains a Category class.
this is my Category class :
class Category : PFObject, PFSubclassing{
override class func load() {
self.registerSubclass()
}
class func parseClassName() -> String! {
return "Category"
}
var nameCategory:String="";
var image:String="";
private let KeyName:String = "label";
private let KeyImage:String = "image";
func fromPFObject(object:PFObject)->Category{
self.nameCategory = object[self.KeyName] as String;
self.image = object[self.KeyImage] as String;
return self;
}
And this is my Event class :
class Event {
//Variables de classe
var name:String;
var category:Category;
// Descripteur de colone en base
private let KeyEventName:String = "event_name";
private let KeyCategory:String = "category";
func insert(){
var event = self.toPFObject()
event.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if (success != false) {
println("Object created with id: \(event.objectId)")
self.id = event.objectId;
} else {
println(error)
}
}
}
func select(id:String){
var query = PFQuery(className: self.KeyClassName)
query.getObjectInBackgroundWithId(id) {
(event: PFObject!, error: NSError!) -> Void in
if (error == nil) {
self.fromPFObject(event);
} else {
println(error)
self.eventKO("Une erreur c'est produite");
}
}
}
func toPFObject()->PFObject{
var event = PFObject(className: self.KeyClassName)
event[self.KeyCategory] = self.category;
event[self.KeyEventName] = self.name
return event;
}
func fromPFObject(event:PFObject)->Event{
self.activ = event[self.KeyActif] as Int;
var test = event[self.KeyCategory] as PFObject;
var label = test["label"] as String;
self.category = event[self.KeyCategory] as Category;
self.name = event[self.KeyEventName] as String;
return self;
}
I get a good retrieve the name of the event but not the category. I have missed something in my models.
Do you have any ideas?
I found it simpler to work directly with the objects instead of subclassing.
Simply refer to properties on your classes as follows:
// non optional of type String that defaults to an empty string
let firstName = object["firstName"] as? String ?? ""
if you have linked classes, e.g. if Category is another class you have a reference to, the following works cleanly
if let category = object["category"] as? PFObject {
let name = category["name"] as? String ?? ""
}
Again, no need to mess around with subclassing everything for simple objects.
I've used this patter to populate PFQueryTableViewControllers, custom tables, custom views etc.
Related
Initially I had the following classes:
#objcMembers public class NormalObjectRealm: Object {
// Shared
dynamic public var id: String?
dynamic public var title: String?
dynamic public var subTitle: String?
dynamic public var imageInfo: ImageInfoRealm?
dynamic public var descriptionString: String?
public var categories = List<String>()
public var count = RealmOptional<Int>()
public var episodes = List<String>()
public static let realmPrimaryKey: String = "id"
public override class func primaryKey() -> String? {
return NormalObjectRealm.realmPrimaryKey
}
}
#objcMembers public class ImageInfoRealm: Object {
dynamic public var id: String?
dynamic public var url: String?
public static let realmPrimaryKey: String = "id"
public override class func primaryKey() -> String? {
return ImageInfoRealm.realmPrimaryKey
}
}
but now NormalObjectRealm is kind of incorporated into a new class like so:
#objcMembers public class MediaObjectRealm: Object {
// Shared
dynamic public var id: String?
dynamic public var title: String?
dynamic public var subTitle: String?
dynamic public var imageInfo: ImageInfoRealm?
dynamic public var descriptionString: String?
public var categories = List<String>()
dynamic public var type: String?
// NormalObjectRealm
public var episodeCount = RealmOptional<Int>()
public var episodes = List<String>()
// OtherObjectRealm
dynamic public var urlOne: String?
dynamic public var urlTwo: String?
dynamic public var urlThree: String?
public var isExplicit = RealmOptional<Bool>()
public static let realmPrimaryKey: String = "id"
public override class func primaryKey() -> String? {
return MediaObjectRealm.realmPrimaryKey
}
}
I'm currently trying to write the migration for the transition here where the idea basically is to transfer most of the fields over from NormalObjectRealm to MediaObjectRealm.
This is what my migration-block currently looks like
Realm.Configuration(schemaVersion: schemaVersion, migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < temp {
print("RealmMigration: Applying migration from \(oldSchemaVersion) to \(temp)")
migration.enumerateObjects(ofType: "NormalObjectRealm") { oldObject, newObject in
guard let oldObject = oldObject else {
return
}
guard let id = oldObject["id"] as? String else {
return
}
guard let title = oldObject["title"] as? String else {
return
}
guard let subTitle = oldObject["subTitle"] as? String else {
return
}
guard let imgInfo = oldObject["imageInfo"] else {
return
}
guard let count = oldObject["count"] as? RealmOptional<Int>? else {
return
}
guard let descriptionString = oldObject["descriptionString"] as? String? else {
return
}
let item = migration.create("MediaObjectRealm")
item["id"] = id
item["title"] = title
item["subTitle"] = subTitle
item["descriptionString"] = descriptionString
item["type"] = "myType"
item["episodeCount"] = episodeCount // Doesn't work either...
migration.enumerateObjects(ofType: "ImageInfoRealm") { oldImg, newImg in
guard let oldImg = oldImg else {
return
}
let inf = oldObject.value(forKey: "imageInfo")
print(inf)
let t = migration.create("ImageInfoRealm", value: inf)
print("doing it")
// print(t)
item.setValue(t, forKey: "imageInfo")
}
}
}
})
id, title, subTitle etc. (String? and Date? variables) are set fine and appear inside the newly created MediaObjectRealm DB-Entries. However imageInfo of type ImageInfoRealm does not... setting it directly like so: item.setValue(oldObject.value(forKey: "imageInfo"), forKey: "imageInfo") (or item["imageInfo"] = oldObject.value(forKey: "imageInfo")) results in realm crashing and telling me that this object is from another realm and I have to copy it over.
'Object is already managed by another Realm. Use create instead to
copy it into this Realm.'
Creating it like in the code above results in not even having any items of type MediaObjectRealm at all i.e. loosing all the data (as NormalObjectRealm is also not present anymore).
Am I missing something? What I basically want is to to take the link/reference from the NormalObjectRealm and copy it to the new MediaObjectRealm.
After long testing and trying different possibilities I managed to migrate the data.
Here is what I did to accomplish this.
I used this as a base:
class RealmMigrationObject {
let migration: () -> ()
init(migration: #escaping () -> ()) {
self.migration = migration
}
}
and derived classes from that. Something like:
class MigrationObjectToThree: RealmMigrationObject {
init() {
super.init(migration: MigrationObjectToThree.migration)
}
private static func migration() {
print("Migration to three | migration")
var imageInfos: [ImageInfo] = []
let config = Realm.Configuration(schemaVersion: 3, migrationBlock: { migration, oldSchemaVersion in
print("Migration to three | migrationBlock")
print("RealmMigration: Applying migration from \(oldSchemaVersion) to 3")
migration.deleteData(forType: "ExploreSectionObjectRealm")
migration.enumerateObjects(ofType: "ImageInfoRealm") { oldInfo, newObject in
guard let oldInfo = oldInfo else {
return
}
guard let id = oldInfo["id"] as? String,
let url = oldInfo["url"] as? String,
let url500 = oldInfo["url500"] as? String,
let url400 = oldInfo["url400"] as? String,
let url300 = oldInfo["url300"] as? String,
let url200 = oldInfo["url200"] as? String,
let url100 = oldInfo["url100"] as? String,
let colorString = oldInfo["color"] as? String,
let color = UIColor(hexString: colorString) else {
return
}
imageInfos.append(ImageInfo(id: id,
url: url,
url500: url500,
url400: url400,
url300: url300,
url200: url200,
url100: url100,
color: color))
}
})
Realm.Configuration.defaultConfiguration = config
do {
let realm = try Realm(configuration: config)
print("Realm is located at: \(realm.configuration.fileURL?.description ?? "")")
print(realm.configuration.fileURL?.description ?? "") // Printing here on purpose as it's easier to copy
} catch {
print("Realm Error: \(error), trying to rebuild realm from scratch")
let deleteMigrationConfig = Realm.Configuration(schemaVersion: RealmHelper.schemaVersion,
deleteRealmIfMigrationNeeded: true)
do {
_ = try Realm(configuration: deleteMigrationConfig)
} catch {
print("Failed to instantiate: \(error.localizedDescription)")
}
}
RealmHelper.removeRealmFiles()
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 3)
imageInfos.forEach({ $0.save() })
}
}
From that I just created all migration for the difference between the current schema version and target schema version on looped over all migrations simply executing the migration function of that given object.
I am facing an issue where I am unable to keep existing relationships after calling add(_, update: true) function.
I wrote a TaskSync class that is responsible for creating/updating Task objects:
class TaskSync: ISync {
typealias Model = Task
func sync(model: Task) {
let realm = try! Realm()
let inWrite = realm.isInWriteTransaction
if !inWrite {
realm.beginWrite()
}
let _task = realm.object(ofType: Task.self, forPrimaryKey: model.id)
// Persist matches as they are not getting fetched with the task
if let _task = _task {
print("matches: \(_task.matches.count)")
model.matches = _task.matches
}
realm.add(model, update: true)
if _task == nil {
var user = realm.object(ofType: User.self, forPrimaryKey: model.getUser().id)
if (user == nil) {
user = model.getUser()
realm.add(user!, update: true)
}
user!.tasks.append(model)
}
if !inWrite {
try! realm.commitWrite()
}
}
func sync(models: List<Task>) {
let realm = try! Realm()
try! realm.write {
models.forEach { task in
sync(model: task)
}
}
}
}
When a model is to be synced, I check if it already exists in the Realm and if so, I fetch it and try to include the matches property as this one is not included in the model.
Right before the call realm.add(model, update: true), model contains list of matches, however right after the realm.add is executed, the matches list is empty.
Here are the two models:
class Task: Object, ElementPreloadable, ElementImagePreloadable, ItemSectionable {
dynamic var id: Int = 0
dynamic var title: String = ""
dynamic var desc: String = ""
dynamic var price: Float = 0.0
dynamic var calculatedPrice: Float = 0.0
dynamic var location: String = ""
dynamic var duration: Int = 0
dynamic var date: String = ""
dynamic var category: Category?
dynamic var currency: Currency?
dynamic var longitude: Double = 0.0
dynamic var latitude: Double = 0.0
dynamic var state: Int = 0
dynamic var userId: Int = 0
// Existing images
var imagesExisting = List<URLImage>()
// New images
var imagesNew = List<Image>()
// Images deleted
var imagesDeleted = List<URLImage>()
private let users = LinkingObjects(fromType: User.self, property: "tasks")
var user: User?
var matches = List<Match>()
dynamic var notification: Notification?
override static func ignoredProperties() -> [String] {
return ["imagesExisting", "imagesNew", "imagesDeleted", "user", "tmpUser"]
}
override static func primaryKey() -> String? {
return "id"
}
func getImageMain() -> URLImage? {
for image in imagesExisting {
if image.main {
return image
}
}
return imagesExisting.first
}
func getSection() -> Int {
return state
}
func getSectionFieldName() -> String? {
return "state"
}
func getId() -> Int {
return id
}
func getURL() -> URL? {
if let image = getImageMain() {
return image.getResizedURL()
}
return nil
}
func getState() -> TaskOwnState {
return TaskOwnState(rawValue: state)!
}
func getUser() -> User {
return (user != nil ? user : users.first)!
}
}
class Match: Object, ElementPreloadable, ElementImagePreloadable, ItemSectionable {
dynamic var id: Int = 0
dynamic var state: Int = -1
dynamic var priorityOwnRaw: Int = 0
dynamic var priorityOtherRaw: Int = 0
dynamic var user: User!
var messages = List<Message>()
private let tasks = LinkingObjects(fromType: Task.self, property: "matches")
var task: Task?
dynamic var notification: Notification?
override static func primaryKey() -> String? {
return "id"
}
override static func ignoredProperties() -> [String] {
return ["task"]
}
func getId() -> Int {
return id
}
func getSection() -> Int {
return 0
}
func getURL() -> URL? {
if let image = user.getImageMain() {
return image.getResizedURL()
}
return nil
}
func getPriorityOwn() -> PriorityType {
if priorityOwnRaw == PriorityType.normal.rawValue {
return PriorityType.normal
}
else {
return PriorityType.favorite
}
}
func getPriorityOther() -> PriorityType {
if priorityOtherRaw == PriorityType.normal.rawValue {
return PriorityType.normal
}
else {
return PriorityType.favorite
}
}
func getSectionFieldName() -> String? {
return nil
}
func getTask() -> Task {
return (task != nil ? task : tasks.first)!
}
}
I spent hours trying to figure out why I am unable to keep the matches relationship when updating the task. Every advice will be highly appreciated!
This question was also asked upon Realm's GitHub issue tracker. For posterity, here is the solution.
List properties should always be declared as let properties, as assigning to them does not do anything useful. The correct way to copy all objects from one List to another is model.tasks.append(objectsIn: _user.tasks).
I'm trying to get my head around optionals, let's say I have the following classes:
class Contact {
var displayName:String
init(displayName:String) {
self.displayName = displayName
}
}
class Contacts {
func create(displayName:String) -> Contact {
return Contact(displayName: displayName)
}
}
as you can see the Contact class has a field displayName, but this value can be nil on initialization.
so to initialize this class I would normally do:
let contact = Contact(displayName: "Test Name")
but instead I want to be able to do this as well:
let contact = Contact()
or
let contact = Contacts().create()
You can make the init parameter an optional String?, with a default
value nil:
class Contact {
var displayName: String?
init(displayName: String? = nil) {
self.displayName = displayName
}
}
let contact1 = Contact()
let contact2 = Contact(displayName: "John")
The same works for the Contacts class:
class Contacts {
func create(displayName: String? = nil) -> Contact {
return Contact(displayName: displayName)
}
}
let contacts = Contacts()
let contact3 = contacts.create()
let contact4 = contacts.create("Mary")
Change your class to this if you want to use an optional displayName:
class Contact {
var displayName: String?
convenience init(displayName: String) {
self.init()
self.displayName = displayName
}
}
This allows you to to this:
let contact = Contact()
let otherContact = Contact(displayName: "Test Name")
EDIT:
Here's the create function as well:
class Contacts {
func create() -> Contact {
return Contact()
}
func create(displayName: String) -> Contact {
return Contact(displayName: displayName)
}
}
But I'd recommend this:
class Contact {
var displayName: String?
init(displayName: String?) {
self.displayName = displayName
}
}
class Contacts {
func create(displayName: String?) -> Contact {
return Contact(displayName: displayName)
}
}
Made the displayName property optional, like so:
var displayName:String?
Optionals can either hold nil or an actual value, in your case a string.
I've looked around and all the other answers do not work for me, I'm trying to query PFUser, but I keep getting an exception of 'The class PFUser must be registered with registerSubclass before using Parse.'
This is where the issue occurs:
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.item]
if self.avatars[message.senderId] == nil {
var imageView = JSQMessagesAvatarImage(placeholder: UIImage(named: "profile"))
self.avatars[message.senderId] = imageView
let user = users[message.senderId]!
print(user)
var parseAvatar = PFFile()
PFUser.registerSubclass()
let imageQuery = PFUser.query()
imageQuery?.whereKey("objectId", equalTo: user.objectId!)
imageQuery?.limit = 1
imageQuery?.findObjectsInBackgroundWithBlock({ (images: [AnyObject]?, error: NSError?) -> Void in
if imageQuery?.countObjects() == 0 {
return
} else {
for image in images! {
if image.objectForKey("profileImage") == nil {
return
} else {
let userPic = image.objectForKey("profileImage") as! PFFile
userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
let image = UIImage(data: imageData!)
imageView.avatarImage = JSQMessagesAvatarImageFactory.circularAvatarImage(UIImage(data: imageData!), withDiameter: 30)
}
})
}
}
}
})
// Reload entire table now that the avatar is downloaded
self.collectionView?.reloadData()
}
return self.avatars[message.senderId]
}
Any idea what I might be missing here? I did this in another file, and it doesn't have that issue. So I'm a little confused.
EDIT:
Like this?
import Foundation
import Parse
class TradeUser : PFUser {
override class func initialize() {
self.registerSubclass()
}
var userID : String {
get {return objectForKey("objectId") as! String}
set { setObject(newValue, forKey: "objectId") }
}
var emailAddress : String {
get {return objectForKey("email") as! String}
set { setObject(newValue, forKey: "email") }
}
var firstName : String {
get {return objectForKey("firstName") as! String}
set {setObject(newValue, forKey: "firstName")}
}
var lastName : String {
get {return objectForKey("lastName") as! String}
set {setObject(newValue, forKey: "lastName")}
}
var primaryQueue : String {
get {return objectForKey("primaryQueue") as! String}
set {setObject(newValue, forKey: "primaryQueue")}
}
var image : PFFile {
get { return self["profileImage"] as! PFFile }
set { self["profileImage"] = newValue }
}
}
It seems like there's an issue with how the subclass is being registered.
Update your PFUser subclass with the following
class TradeUser : PFUser {
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
...
}
I have a User Struct that I'm casting to Json to be able to get into NSUserDefaults...
import Foundation
struct User {
var name = ""
var stores: [Store] = []
init?(json: [String: AnyObject]) {
if let name = json["name"] as? String,
storesJSON = json["stores"] as? [[String: AnyObject]]
{
self.name = name
self.stores = storesJSON.map { Store(json: $0)! }
} else {
return nil
}
}
init() { }
func toJSON() -> [String: AnyObject] {
return [
"name": name,
"stores": stores.map { $0.toJSON() }
]
}
}
and I am using a Data Manager class (Singleton) to add a new User. But I can't figure out what to pass into updateValue in my addPerson function below? Alternatively is there another way to get this object into NSUserDefaults?
import Foundation
class DataManager {
static let sharedInstance = DataManager()
var users = [String : User]()
init() {
let userDefaults = NSUserDefaults.standardUserDefaults()
if let var userFromDefaults = userDefaults.objectForKey("users") as? [String : User] {
users = userFromDefaults
}
else {
// add default values later
}
}
var userList: [String] {
var list: [String] = []
for userName in users.keys {
list.append(userName)
}
list.sort(<)
return list
}
func addPerson(newUserName: String) {
users.updateValue(User(), forKey: newUserName)
// saveData()
}
You should change your interface of the addPerson function, use addPerson(newUser: User) instead of using addPerson(newUserName: String) as #iosDev82 said:
// Because your addPerson function needs two parameters: a name and a user object
func addPerson(newUser: User) {
users.updateValue(newUser, forKey: newUser.name)
// saveData()
}
so you can:
let newName = textField.text.capitalizedString
let newUser = User(["name": newName, "stores" : []])
DataManager.sharedInstance.addPerson(newUser)
I think you already know how to create a User object. And that is what you should pass as an argument to your following function. Something like this.
var aUser = User(["name": textField.text. capitalizedString])
DataManager.sharedInstance.addPerson(aUser)
func addPerson(newUser: User) {
users[newUser.name] = newUser
// saveData()
}