Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving unmanaged object throws exception #7786

Closed
versoworks opened this issue May 7, 2022 · 5 comments · Fixed by #7790
Closed

Saving unmanaged object throws exception #7786

versoworks opened this issue May 7, 2022 · 5 comments · Fixed by #7790

Comments

@versoworks
Copy link

versoworks commented May 7, 2022

How frequently does the bug occur?

All the time

Description

Recently updated Realm from v10.7.6 to v10.25.2 and code that was previously working now throws an exception;

Object has been deleted or invalidated

Specifically, the exception is raised when saving an unmanaged object.

Stacktrace & log output

0x0000000109168884 in RLMVerifyAttached(RLMObjectBase*) at /Users/admin/Documents/Code/app/Pods/Realm/include/RLMObject_Private.hpp:42
0x00000001091684dc in RLMDynamicValidatedSet at /Users/admin/Documents/Code/app/Pods/Realm/Realm/RLMAccessor.mm:670
0x00000001092449e0 in RLMObjectBaseSetObjectForKeyedSubscript at /Users/admin/Documents/Code/app/Pods/Realm/Realm/RLMObjectBase.mm:459
0x000000010923f108 in -[RLMObject setObject:forKeyedSubscript:] at /Users/admin/Documents/Code/app/Pods/Realm/Realm/RLMObject.mm:96
0x0000000105a1f2e4 in __42-[appContentRepository addOrUpdateObject:]_block_invoke at /Users/admin/Documents/Code/app/appKit/appContentRepository.m:387
0x00000001093c889c in -[RLMRealm transactionWithoutNotifying:block:error:] at /Users/admin/Documents/Code/app/Pods/Realm/Realm/RLMRealm.mm:729
0x00000001093c8724 in -[RLMRealm transactionWithBlock:error:] at /Users/admin/Documents/Code/app/Pods/Realm/Realm/RLMRealm.mm:720
0x00000001093c868c in -[RLMRealm transactionWithBlock:] at /Users/admin/Documents/Code/app/Pods/Realm/Realm/RLMRealm.mm:716
0x0000000105a1f110 in -[appContentRepository addOrUpdateObject:] at /Users/admin/Documents/Code/app/appKit/appContentRepository.m:376
0x0000000104c1f9dc in -[appContentViewModel saveappContentSearchHistory:] at /Users/admin/Documents/Code/app/app/appContentViewModel.m:169
0x0000000104c091e8 in -[appContentVC execSearch:] at /Users/admin/Documents/Code/app/app/appContentVC.m:771
0x0000000104c16048 in -[appContentVC textFieldShouldReturn:] at /Users/admin/Documents/Code/app/app/appContentVC.m:2326
0x000000019bc30890 in -[UITextField keyboardInput:shouldInsertText:isMarkedText:] ()
0x000000019b8027f4 in -[UIKeyboardImpl callShouldInsertText:] ()
0x000000019b815304 in -[UIKeyboardImpl addWordTerminator:afterSpace:afterAccepappgCandidate:elapsedTime:executionContext:] ()
0x000000019b01fe78 in -[UIKeyboardTaskExecutionContext returnExecutionToParentWithInfo:] ()
0x000000019b813aa4 in -[UIKeyboardImpl acceptAutocorrectionForWordTerminator:executionContextPassingTIKeyboardCandidate:] ()
0x000000019b81335c in -[UIKeyboardImpl addInputEvent:executionContext:] ()
0x000000019b812e8c in __81-[UIKeyboardImpl addInputString:withFlags:withInputManagerHint:executionContext:]_block_invoke ()
0x000000019b01fe78 in -[UIKeyboardTaskExecutionContext returnExecutionToParentWithInfo:] ()
0x000000019b812e48 in -[UIKeyboardImpl addInputString:withFlags:withInputManagerHint:executionContext:] ()
0x000000019b825c24 in -[UIKeyboardImpl handleKeyWithString:forKeyEvent:executionContext:] ()
0x000000019b7fcb74 in -[UIKeyboardImpl performOperations:withTexappputSource:] ()
0x000000019b825458 in -[UIKeyboardImpl handleKeyEvent:executionContext:] ()
0x000000019b8247d8 in __33-[UIKeyboardImpl handleKeyEvent:]_block_invoke ()
0x000000019b8245d4 in -[UIKeyboardImpl _handleKeyEvent:executionContext:] ()
0x000000019b77e128 in -[UIKeyboardLayoutStar completeRetestForTouchUp:timestamp:interval:executionContext:] ()
0x000000019b82e4f8 in __45-[UIKeyboardLayout touchUpTaskForTouchState:]_block_invoke ()
0x000000019b82e63c in __28-[UIKeyboardLayout touchUp:]_block_invoke ()
0x000000019b047e9c in -[UIKeyboardTaskEntry execute:] ()
0x000000019aef4b44 in -[UIKeyboardTaskQueue conappueExecutionOnMainThread] ()
0x000000019ac03008 in -[UIKeyboardLayout touchUp:] ()
0x000000019b82e208 in -[UIKeyboardLayout _touchEndedProcessingForTouches:] ()
0x000000019ac19e60 in -[UIWindow _sendTouchesForEvent:] ()
0x000000019ac4b100 in -[UIWindow sendEvent:] ()
0x000000019adf8ae4 in -[UIApplication sendEvent:] ()
0x000000019ac1e9ec in __dispatchPreprocessedEventFromEventQueue ()
0x000000019ac1371c in __processEventQueue ()
0x000000019ba63470 in updateCycleEntry ()
0x000000019b281084 in _UIUpdateSequenceRun ()
0x000000019b906cb0 in schedulerStepScheduledMainSection ()
0x000000019b906478 in runloopSourceCallback ()
0x00000001986f3f04 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
0x0000000198704c90 in __CFRunLoopDoSource0 ()
0x000000019863e184 in __CFRunLoopDoSources0 ()
0x0000000198643b4c in __CFRunLoopRun ()
0x00000001986576b8 in CFRunLoopRunSpecific ()
0x00000001b46f1374 in GSEventRunModal ()
0x000000019afbce88 in -[UIApplication _run] ()
0x000000019ad3e5ec in UIApplicationMain ()
0x0000000104b810ec in main at /Users/admin/Documents/Code/app/app/main.m:16
0x0000000105351ce4 in start ()

Can you reproduce the bug?

Yes, always

Reproduction Steps

Pseudo code that results in the exception;

AppContentSearchHistory *history = [[AppContentSearchHistory alloc] init];
history.text = query;
[viewModel saveAppContentSearchHistory:history];
history = nil;

The viewModel call leads to an abstract class method to save the object, which is where the exception is raised;

- (void) addOrUpdateObject:(RLMObject*) object 
{
  [realm transactionWithBlock:^
      {
          if (!object[@"_id"])
          {
              object[@"_id"] = [RLMObjectId objectId];
          }
          
          if (!object[@"_partition"])
          {
              object[@"_partition"] = partitionId; // ****EXCEPTION****
          }
          
          [realm addOrUpdateObject:object];
      }];
}

We can work around the exception with the following change to the addOrUpdateObject method;

        if (!object[@"_partition"])
        {
            if (object.realm)
            {
                object[@"_partition"] = partitionId;
            }
            else
            {
                [object setValue:partitionId forKey:@"_partition"];
            }
        }

Version

10.25.2

What SDK flavour are you using?

MongoDB Realm (i.e. Sync, auth, functions)

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 15.4.1

Build environment

Xcode version: 13.3.1
Dependency manager and version: Cocoapods v1.11.2

@tgoyne
Copy link
Member

tgoyne commented May 7, 2022

You're hitting the object->_realm || object.class == object->_objectSchema.accessorClass branch in RLMObjectBaseSetObjectForKeyedSubscript(), and it'd be helpful to know what the value of all of those are. For unmanaged objects, object->_realm should be nil, class_getName(object.class) should be "AppContentSearchHistory", and class_getName(object->_objectSchema.accessorClass) should be something like "RLM:Managed 1 AppContentSearchHistory" (the actual number will vary). Note that just printing the values in the debugger without class_getName() will print the prettified names and not the actual ones.

@versoworks
Copy link
Author

@tgoyne here's the object print out;

Printing description of object:
(RLMObject) RLMObject = {
  RLMObjectBase = {
    NSObject = {
      isa = AppContentSearchHistory
    }
    _row = {
      m_table = {
        realm::ConstTableRef = {
          m_table = nil
          m_instance_version = 0
        }
      }
      m_key = (value = -1)
      m_mem = (m_addr = 0x0000000000000000, m_ref = 0)
      m_row_ndx = 18446744073709551615
      m_storage_version = 18446744073709551615
      m_valid = false
    }
    _observationInfo = nil
    _info = nil
    _realm = nil
    _objectSchema = 0x00000002810fa880
    _lastAccessedNames = nil
  }
}

Printing description of object->_objectSchema:
_isSwiftClass	bool	false
_objectStoreName	std::string	""	
_isEmbedded	BOOL	NO	false
_properties	__NSArrayI *	4 elements	0x000000028305f3c0
_className	__NSCFString *	"AppContentSearchHistory"	0x0000000283e92e60
_primaryKeyProperty	RLMProperty *	0x2815de0d0	0x00000002815de0d0
_allPropertiesByName	__NSDictionaryM *	4 key/value pairs	0x0000000283e84720
_objectClass	Class	AppContentSearchHistory	0x0000000102d25948
_accessorClass	Class	AppContentSearchHistory	0x0000000102d25948
_unmanagedClass	Class	AppContentSearchHistory	0x0000000102d25948
_computedProperties	__NSArray0 *	0 elements	0x00000001f2718000
_swiftGenericProperties	NSArray *	nil	0x0000000000000000

@tgoyne
Copy link
Member

tgoyne commented May 7, 2022

Are you opening your Realm with objectClasses set on your config? The schema there doesn't have the initialization done which happens when a Realm is first opened, so perhaps we have a bug related to that.

@versoworks
Copy link
Author

@tgoyne yes, objectClasses is set on the Realm config.

@tgoyne
Copy link
Member

tgoyne commented May 7, 2022

Okay, I think I can see why this'd be breaking then.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants