ios


Deleting Data instead of using Migration


Deleting Data instead of using Migration
In my code I added a property to my realm class. Is there a way to delete the data and get rid off my whole database instead of using migration?
I'm still testing that's why I probably won't need Migration for now.
Please help me, I'm really struggling with using Migration
Thank you
if you are only testing locally you can delete the app and re-install. If you already have your app on the App Store and want to prepare migrations for users you can have a look at the Migrations Sample App from Realm.
Try this on your AppDelegate:
import UIKit
import RealmSwift
// Old data models
/* V0
class Person: Object {
dynamic var firstName = ""
dynamic var lastName = ""
dynamic var age = 0
}
*/
// V1
class Person: Object {
dynamic var fullName = "" // combine firstName and lastName into single field
dynamic var age = 0
}
class Person: Object {
dynamic var fullName = ""
dynamic var age = 0
}
func bundleURL(_ name: String) -> URL? {
return Bundle.main.url(forResource: name, withExtension: "realm")
}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
// copy over old data files for migration
let defaultURL = Realm.Configuration.defaultConfiguration.fileURL!
let defaultParentURL = defaultURL.deletingLastPathComponent()
if let v0URL = bundleURL("default-v0") {
do {
try FileManager.default.removeItem(at: defaultURL)
try FileManager.default.copyItem(at: v0URL, to: defaultURL)
} catch {}
}
// define a migration block
// you can define this inline, but we will reuse this to migrate realm files from multiple versions
// to the most current version of our data model
let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
// combine name fields into a single field
let firstName = oldObject!["firstName"] as! String
let lastName = oldObject!["lastName"] as! String
newObject?["fullName"] = "\(firstName) \(lastName)"
}
}
}
print("Migration complete.")
}
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 2, migrationBlock: migrationBlock)
// print out all migrated objects in the default realm
// migration is performed implicitly on Realm access
print("Migrated objects in the default Realm: \(try! Realm().objects(Person.self))")
return true
}
}

Related Links

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.
Writing a flood-fill method in Swift
Facebook Graph API query for latest album from page
Options for sending data to server/retrying requests in background
xcode, ios, Help Image Freezes, Not Moving
Hidden property cannot be changed within an animation block
MPmoviePlayer going black after updating to IOS 9 ( swift )
Run custom script after iOS app is signed in XCode
Video frames fill only 1/4 of the view - GL
How can I change the image in the detail view to access the master view
Can I use swift 1.2 with iOS 9? [closed]
Why same nsmutablearray gives me valid value and a null on different functions used
Capture location in all states app
Does prefix works correctly in Xcode 7 beta 6?
Ios Push notification : Null would like to send you notifications
Programming Transition Loses Navigation Bar

Categories

HOME
mocking
coffeescript
compiler-errors
path
word-vba
garbage-collection
genetic-algorithm
api.ai
go-gorm
jasypt
windows-server-2012-r2
classloader
socrata
plotly
h2o
stm
jquery-jtable
vscode-settings
postgresql-9.5
asp.net-mvc-5.2
condor
next
eigen3
graphcool
jmeter-plugins
spring-restdocs
semantics
aikau
aspdotnetstorefront
sax
ratchet-2
typemock-isolator
structure
rrd
effects
coqide
apache-commons-httpclient
skyscanner
sqlite.swift
outlook-vba
jackrabbit
draggable
presto
mask
json-rpc
ecdsa
runc
drupal-webform
rft
pac
google-slides
btle
formatdatetime
ios-app-group
htc
assembly-references
qtcpserver
vs-2015-preview
nsd
ipopt
printscreen
texture2d
namenode
enhanced-ecommerce
oracle-policy-automation
ctakes
flexigrid
dual-table
openejb
python-2.3
oboe.js
realm-list
equinox
abort
adserver
nolio
iscroll4
culture
jet.com-apis
django-postgresql
bessel-functions
first-class-functions
spread-toolkit
validform
blueprint
onejar
libtorrent-rasterbar
buildout
sparc
icon-fonts
osmf
12factor
symfony-2.2
guvnor
mysql-backup
zmodem
ria
sticky-footer
quilt
gemstone
sdl.net
having
directoryentry
net-ssh
gedcom
sqlitemanager
sqlprofileprovider

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App