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

Xcode autolayout - dynamic constraints
How to solve the error “:37: parser error : EntityRef: expecting ';'” in Xcode, Objective-C
iOS new view not appearing and/or old view not disappering
How to place textfield position using tap gesture
UICollectionView must be initialized with a non-nil layout parameter. error in swift with UICollectionView
How to rotate custom marker image on google map objective c
Determine “nil” value in JSON
How to Speed up the audio Playing in MpMoviePlayerController
share extension in ios 8 with objective c
Implementing pixel-perfect background images in different orientations
iAP buy button with price tier text
Segmentation fault OR Stacktrace when using UIView with message forwarding feature of Objective-C
How to get the String Value in JSON
Add TapGestureRecogniser to UITextView
Navigation title not working properly
Chaning height of UIView

Categories

HOME
vb.net
f#
groovy
google-search
servlets
order
prebuild
asana
xcode8
gluon
c++14
android-bluetooth
cross-domain
copy-paste
overloading
proftpd
scope
coreos
facebook-opengraph
python-2.5
rgb
jquery-selectors
graphcool
oracle11gr2
media-source
pwm
semantics
missingmethodexception
py4j
stylesheet
simple-html-dom
text-parsing
f2py
parallax.js
ocean
popupwindow
pymssql
edit-distance
sieve-of-eratosthenes
extraction
internet-explorer-10
wso2iot
plasticscm
angular2-seed
mediawiki-api
php-deployer
gretl
keyboard-maestro
opal
android-gps
mashery
playback
angular-directive
breach-attack
mysql-notifier
mink
printscreen
facebook-audience-network
texture2d
tag-helpers
filenet-content-engine
nominatim
mtp
heroku-toolbelt
selectsinglenode
translate3d
cbc-mode
spatial-index
epplus-4
centering
slick2d
gnat
django-postgresql
aps
twill
dataformat
populate
jqgrid-php
angularjs-google-maps
windows-phone-7.1
cfile
execcommand
gora
xcache
wow64
xhtml-transitional
adlds
jcycle
oncreate
vim-fugitive
ms-access-97
letter-spacing
datatemplate
demoscene
eye-detection
etrade-api
codi
executescalar
posterous
eclipse-tptp
variable-names
greensoftware

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