当前位置: 首页 > 面试题库 >

领域因RLMException崩溃:对象已被删除或无效

卫景明
2023-03-14
问题内容

我有一个存储时间线的领域模型(我正在制作视频编辑应用程序),并且经常访问它的RMArray属性时会迷恋它。该应用程序已经发货,我本人还没有体验过,但是我的美眉经常将崩溃通知我。这是崩溃日志:

Fatal Exception: RLMException
Object has been deleted or invalidated.

Thread : Fatal Exception: RLMException
0  CoreFoundation                 0x2614d45f __exceptionPreprocess + 126
1  libobjc.A.dylib                0x3407ec8b objc_exception_throw + 38
2  VideoEditor               0x00293919 RLMGetArray(RLMObjectBase*, unsigned int, NSString*) (RLMRealm_Private.hpp:38)
3  VideoEditor               0x0018a1b4 VideoEditor.RLMProject.setTimeLineModel (VideoEditor.RLMProject)(VideoEditor.TimeLineModel, beginWriteTransaction : Swift.Bool) -> () (RealmModels.swift:147)
4  VideoEditor               0x0025eb9c VideoEditor.VideoEditorAPI.saveProject (VideoEditor.VideoEditorAPI)(Swift.Optional<VideoEditor.IProject>, timeLine : VideoEditor.TimeLineModel, name : Swift.String, filterID : Swift.Int, image : ObjectiveC.UIImage) -> Swift.ImplicitlyUnwrappedOptional<VideoEditor.IProject> (VideoEditorAPI.swift:42)
5  VideoEditor               0x00164754 @objc VideoEditor.ProjectEditorViewController.saveProject (VideoEditor.ProjectEditorViewController)(Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSNotification>) -> () (ProjectEditorViewController.swift:514)
6  CoreFoundation                 0x26105e31 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
7  CoreFoundation                 0x260616cd _CFXNotificationPost + 1784
8  Foundation                     0x26db7dd9 -[NSNotificationCenter postNotificationName:object:userInfo:] + 72
9  UIKit                          0x296cae2d -[UIApplication _deactivateForReason:notify:] + 528
10 UIKit                          0x298d2dd7 -[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:] + 1846
11 UIKit                          0x298caafd -[UIApplication workspace:didReceiveActions:] + 80
12 FrontBoardServices             0x2ca180a9 __31-[FBSSerialQueue performAsync:]_block_invoke + 12
13 CoreFoundation                 0x26113fe5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
14 CoreFoundation                 0x261132a9 __CFRunLoopDoBlocks + 216
15 CoreFoundation                 0x26111de3 __CFRunLoopRun + 1714
16 CoreFoundation                 0x2605f3b1 CFRunLoopRunSpecific + 476
17 CoreFoundation                 0x2605f1c3 CFRunLoopRunInMode + 106
18 GraphicsServices               0x2d5bf201 GSEventRunModal + 136
19 UIKit                          0x296c943d UIApplicationMain + 1440
20 MerryVideoEditor               0x0028c88f main (main.m:16)
21 libdyld.dylib                  0x3460aaaf start + 2

这是RLMProject代码:

protocol IProject{
   var name: String { get set }
   var filterID: Int { get set }
   var filterIntensity: CGFloat { get set }

   /// duration in seconds
   var duration: Int { get set }
   var dateCreated: NSDate { get }

   func setTimeLineModel(timeLine: TimeLineModel, beginWriteTransaction: Bool)

   //    should be done by ProjectImporter
   func getTimeLineModel() -> TimeLineModel

   var videoAssets: RLMArray { get }
   var soundtracks: RLMArray { get }       
}

class RLMProject: RLMObject, IProject, Printable {
   dynamic var videoAssets: RLMArray = RLMArray(objectClassName: RLMMediaAsset.className())
   dynamic var soundtracks: RLMArray = RLMArray(objectClassName: RLMMediaAsset.className())

   dynamic var name: String = ""

   dynamic var filterID: Int = 0
   dynamic var filterIntensity: CGFloat = 1

   dynamic var duration: Int = 0
   dynamic var dateCreated: NSDate = NSDate()

   dynamic var idValue: Int = 0

   func setTimeLineModel(timeLine: TimeLineModel, beginWriteTransaction: Bool = true) {
      func updateArray(array: RLMArray, withAssetsArray assetsArray: [MediaAsset], type: MediaType){
         array.removeAllObjects()
         for asset in assetsArray{
            let model = RLMMediaAsset()
            model.setMediaAsset(asset)
            model.setType(type)
            array.addObject(model)
            RLMRealm.defaultRealm().addObject(model)
         }
      }
      if beginWriteTransaction { RLMRealm.defaultRealm().beginWriteTransaction() }

      if videoAssets.invalidated { videoAssets = RLMArray(objectClassName: RLMMediaAsset.className()) }
      if soundtracks.invalidated { soundtracks = RLMArray(objectClassName: RLMMediaAsset.className()) }

      updateArray(videoAssets, withAssetsArray: timeLine.videoAssets, .Video)
      updateArray(soundtracks, withAssetsArray: timeLine.soundtracks, .Soundtrack)
      duration = Int(CMTimeGetSeconds(timeLine.totalDuration))

      dateCreated = NSDate()
      if beginWriteTransaction { RLMRealm.defaultRealm().commitWriteTransaction() }
   }

   func getTimeLineModel() -> TimeLineModel {
      let timeLine = TimeLineModel()
      timeLine.videoAssets = videoAssets.map { ($0 as RLMMediaAsset).getMediaAsset() }
      timeLine.soundtracks = soundtracks.map { ($0 as RLMMediaAsset).getMediaAsset() }

      return timeLine
   }
}

extension RLMArray {   
    func map<U>(transform: (RLMObject) -> U) -> [U]{
        var array: [U] = []
        for object in self{
            array.append(transform(object))
        }
        return array
    }
}

有人知道我的代码有什么问题吗?


问题答案:

RLMProject本身失效时,将无法检查videoAssets.invalidatedsoundtracks.invalidated,这就是为什么您的堆栈跟踪显示未捕获的异常被抛出的原因RLMGetArray

这意味着在调用之前,该对象已从领域中删除setTimeLineModel。请realm.deleteObject(_:)在您的代码中查找,以了解可能发生的情况。

最后,一些通用的技巧可以使您的代码更安全,更简单:

而不是RLMRealm.defaultRealm()在您内部的任何地方RLMObject,都应该使用它的realm属性。这样,如果您决定在某个时候更改领域的位置,那么您内部的代码RLMObject将继续起作用。

另外,RLMArray您可以使用Swift的free map函数,而不是在添加地图时创建扩展名。

map(videoAssets) { ($0 as RLMMediaAsset).getMediaAsset() }


 类似资料:
  • 问题内容: 启动应用程序时,我将执行API调用以查看是否有可用的新数据。数据存储在我的本地Realm数据库中,其中一些显示在初始表视图控制器中。 API调用完成后,我将检查是否满足某些条件,这些条件要求我从数据库中删除一堆先前的数据,然后创建新对象。但是,当我删除旧数据时,我的应用程序崩溃,但出现以下异常: 该调用堆栈让我假设,这是由于我在FavoriteLeaguesViewController

  • 注意:我的问题被关闭之前,我尝试了这里提到的解决方案-未知错误:会话删除,因为页面崩溃从未知错误:无法确定加载状态从标签崩溃与ChromeDriver硒-添加这些选项-options.add参数("--disable-dev-shm-用法");options.add参数("--no-沙盒"); -- 我仍然看到的问题 在selenium网格中运行时,在某些情况下,单个测试会出现以下错误: 方法1:

  • 问题内容: 我可以选择进行迁移,但是我希望删除defaultRealm()中的所有内容。我如何轻松做到这一点? 是.deleteObjects唯一的函数。 我尝试了以下代码: 方法一 方法2 两者都无法防止迁移异常。 问题答案: 用途:

  • date String ID Integer

  • 日期 字符串 id String CrashReport Object date Date id String

  • 日期 字符串 id String