title: Dealing with Realm transactions
body: <p>I have an <strong>RESTful</strong> <strong>API</strong> from which I retrieve a large set of data. I am persisting it locally using <strong>Realm</strong> and the following call:</p>
  
  <pre><code>func addObjectType(object: ObjectType){
      // Check for existence of data
      if (realm.object(ofType: ObjectType.self, forPrimaryKey: object.id) == nil) {
          // Persist your data easily
          try! realm.write {
              realm.add(object)
          }
      }
  }
  </code></pre>
  
  <p>The app has a feature to delete the data locally. I have implemented it as <strong>following</strong>:</p>
  
  <pre><code>func deleteAllData() {
      if(!realm.isEmpty){
          do{
              if(!realm.isInWriteTransaction) {
                  realm.beginWrite()
                  realm.deleteAll()
                  try! realm.commitWrite()
              }
          }
          NotificationCenter.default.post(name: Notification.Name("updateUI"), object: nil)
      }
  }
  </code></pre>
  
  <p>However, looking at the <strong>Realm</strong> documentation I see the following:</p>
  
  <blockquote>
    <p>Indicates whether the Realm is currently in a write transaction.</p>
    
    <p><strong>Warning</strong></p>
    
    <p>Do not simply check this property and then start a write transaction
    whenever an object needs to be created, updated, or removed. Doing so
    might cause a large number of write transactions to be created,
    degrading performance. Instead, always prefer performing multiple
    updates during a single transaction.</p>
  </blockquote>
  
  <p>Is my implementation correct? </p>
  
  <p>I feel that I am missing some <strong>checks</strong>..</p>