Our current app is using CloudKit enabled CoreData.
Recently, we want to support Widget feature.
We understand that in order to support Widget feature, we need to place our CoreData under AppGroup, so that the data can be accessed by Widget.
May I know, is there any good guideline on how to do so, so that we would not cause data loss for existing users?
Thank you.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
When come to circular reference, it comes with risk of memory leaking, by not using a weakkeyword. For instance :-
Memory leak without using weak
class Human {
deinit {
print("bye bye from Human")
}
init(_ pet: Pet) {
self.pet = pet
}
let pet: Pet
}
class Pet {
deinit {
print("bye bye from Pet")
}
var human: Human?
}
print("start of scope")
if true {
let pet = Pet()
let human = Human(pet)
pet.human = human
print("going to end of scope")
}
print("end of scope")
/*
Output:
start of scope
going to end of scope
end of scope
*/
No memory leak by using weak
class Human {
deinit {
print("bye bye from Human")
}
init(_ pet: Pet) {
self.pet = pet
}
let pet: Pet
}
class Pet {
deinit {
print("bye bye from Pet")
}
weak var human: Human?
}
print("start of scope")
if true {
let pet = Pet()
let human = Human(pet)
pet.human = human
print("going to end of scope")
}
print("end of scope")
/*
Output:
start of scope
going to end of scope
bye bye from Human
bye bye from Pet
end of scope
*/
In CoreData, when setup 2 entities with one-to-many relationship, it is recommended to have inverse relationship too. Hence, CoreData will generate the following class with circular reference.
extension NSHolidayCountry {
@nonobjc public class func fetchRequest() -> NSFetchRequest<NSHolidayCountry> {
return NSFetchRequest<NSHolidayCountry>(entityName: "NSHolidayCountry")
}
@NSManaged public var code: String
@NSManaged public var name: String
@NSManaged public var holidaySubdivisions: NSOrderedSet
}
extension NSHolidaySubdivision {
@nonobjc public class func fetchRequest() -> NSFetchRequest<NSHolidaySubdivision> {
return NSFetchRequest<NSHolidaySubdivision>(entityName: "NSHolidaySubdivision")
}
@NSManaged public var code: String
@NSManaged public var name: String
@NSManaged public var holidayCountry: NSHolidayCountry?
}
NSHolidaySubdivision is having inverse relationship to NSHolidayCountry.
However, such inverse relationship is not marked as weak, based on CoreData generated class.
I was wondering, does this come with a memory leak risk? Should I, add a weak keyword manually in entity NSHolidaySubdivision's holidayCountry ?
I am using the following mechanism, to perform UITableView's row show and hide.
class TableViewController: UITableViewController {
private var hiddenIndexPaths : Set<IndexPath> = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func toggle(_ sender: UISwitch) {
if sender.isOn {
show(1)
show(2)
} else {
hide(1)
hide(2)
}
tableView.beginUpdates()
tableView.endUpdates()
}
private func isHidden(_ indexPath: IndexPath) -> Bool {
hiddenIndexPaths.contains(indexPath)
}
private func hide(_ item: Int) {
hiddenIndexPaths.insert(IndexPath(item: item, section: 0))
}
private func show(_ item: Int) {
hiddenIndexPaths.remove(IndexPath(item: item, section: 0))
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isHidden(indexPath) {
return 0.0
}
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
As you can see, it works great in iPhone SE simulator (Works well in iPhone SE real device too)
iPhone SE simulator
linkText
However, in non iPhone SE simulator (Like iPhone 13), once the table row is hidden, it can no longer be shown. Please refer to the following video.
iPhone 13 simulator
I am not sure what will its behavior in iPhone 13 real device, because I do not have access.
I was wondering, do you have any idea why such issue occurs?
If you are interested to test, here's the complete workable sample - https://github.com/yccheok/show-hide-table-row-bug
Is there any static analysis tool which can help us detect memory leak cause by missing [weak self]?
I was wondering, is there any tool, which can help to detect memory leak caused by missing [weak self].
For instance, the following code contains memory leak issue caused by lack of [weak self]
class ImagePageViewController: UIPageViewController {
lazy var memoryLeak = UIAction(
title: "memory_leak",
image: nil
) { _ in
print(">>>> \(self)")
}
}
Is there any tool which can help us to prevent such issue? I have tried
https://github.com/realm/SwiftLint
but it is not able to detect such.
We plan to develop a Share extension.
The CoreData is storing its SQLite file in AppGroup folder.
The share extension will run in a different process than main app's.
Since share extension and main app are running in different process, both will have their own instance of CoreData. However, even there are multiple instances of CoreData in different processes, their underlying are pointing to a single same SQLite file.
Under https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1
It mentions
When you set up a shared container, the containing app—and each contained app extension that you allow to participate in data sharing—have read and write access to the shared container. To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container.
But it isn't clear, what are detailed steps required to
How can we synchronize access to CoreData among 2 processes?
Who should responsible to consume relevant store change, and update the single token file? (https://developer.apple.com/documentation/coredata/consuming_relevant_store_changes) Should it be main app, share extension or both?
Thank you.
The following is a code example from widget extension.
By using .environment(.colorScheme, ...), I am able to update view with correct theme aware named color.
However, I am not able to retrieve the correct RGB value, from the theme aware named color.
private func getColorScheme() -> ColorScheme {
if ... {
return ColorScheme.dark
} else {
return ColorScheme.light
}
}
@ViewBuilder
func contentView() -> some View {
// Light/ dark theme aware
let color = SwiftUI.Color("yellowNoteColor")
calculate(color)
HStack {
...
}
.background(color)
.environment(\.colorScheme, getColorScheme())
}
func calculate(_ color: SwiftUI.Color) {
var a: CGFloat = 0.0
var r: CGFloat = 0.0
var g: CGFloat = 0.0
var b: CGFloat = 0.0
let uiColor = UIColor(self)
uiColor.getRed(&r, green: &g, blue: &b, alpha: &a)
// !!! Always get the light theme color value !!!
}
Inside calculate function, the retrieved value is always in the light theme color value.
My guess is, caculate function is executed before .environment(\.colorScheme, getColorScheme()), that's why we are getting light theme color value always.
May I know, how to get correct RGB value from theme aware named color?
My app starts to introduce AdMob.
In App Store Connect page, when I perform editing under App Privacy/ Data Types
The input choice will be
applicable to both "Data Used to Track You" and "Data Not Linked to You"
Does anyone know how we can only edit "Data Not Linked to You" only?
Am I doing something not right?
Also, I notice that I am not allow to uncheck "Device ID" and then submit (For testing purpose).
Is it because I am using NSUserTrackingUsageDescription in my app?
I would like to implement in-note text search feature, as found in Apple's Notes, Apple's Safari app. It looks like the following
I understand that such an API is called UIFindInteraction, and only available in iOS16.
https://developer.apple.com/documentation/uikit/uifindinteraction
WWDC 2022: Adopt desktop-class editing interactions
However, my app is targeting iOS 15.
I was wondering, is it possible for us to provide same feature, in iOS 15?
Thank you.
I was able to avoid NSInternalInconsistencyException by using
NSDiffableDatasource with NSFetchedResultsController
My data source definition is
private typealias DataSource = UICollectionViewDiffableDataSource<Int, NSManagedObjectID>
(Does anyone know why we should use Int as SectionIdentifierType? I find it works fine too, if I am using String as SectionIdentifierType)
Even though I do not see NSInternalInconsistencyException anymore, I have the following crash log from Firebase Crashlytics. Do you know how to fix that?
Fatal Exception: NSRangeException
0 CoreFoundation 0x99288 __exceptionPreprocess
1 libobjc.A.dylib 0x16744 objc_exception_throw
2 CoreFoundation 0x1a4318 -[__NSCFString characterAtIndex:].cold.1
3 CoreFoundation 0x928c8 -[NSArray subarrayWithRange:]
4 CoreData 0x702dc -[_PFArray subarrayWithRange:]
5 CoreData 0xc2d58 -[_NSDefaultSectionInfo objects]
6 CoreData 0x14eb98 -[NSFetchedResultsController _conditionallyDispatchSnapshotToDelegate:updatesInfo:]
7 CoreData 0xc3edc -[NSFetchedResultsController performFetch:]
My NSFetchedResultsController look as following
private lazy var fetchedResultsController: NSFetchedResultsController<NSPlainNote> = {
let fetchRequest: NSFetchRequest<NSPlainNote> = NSPlainNote.fetchRequest(
label: label,
propertiesToFetch: ["pinned"]
)
let controller = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: CoreDataStack.INSTANCE.viewContext,
sectionNameKeyPath: "pinned",
cacheName: nil
)
controller.delegate = fetchedResultsControllerDelegate
return controller
}()
My NSPlainNote.fetchResult looks as following
static func fetchRequest(label: String?, propertiesToFetch: [Any]?) -> NSFetchRequest<NSPlainNote> {
let fetchRequest = NSPlainNote.fetchRequest()
fetchRequest.propertiesToFetch = propertiesToFetch
fetchRequest.sortDescriptors = [
NSSortDescriptor(key: "pinned", ascending: false),
NSSortDescriptor(key: "order", ascending: true)
]
if let label = label {
let predicate = NSPredicate(format: "archived = false AND trashed = false AND label = %@", label)
fetchRequest.predicate = predicate
} else {
let predicate = NSPredicate(format: "archived = false AND trashed = false")
fetchRequest.predicate = predicate
}
return fetchRequest
}
I am not able to reproduce the problem.
However, we can observe crash happens during NSFetchedResultsController.performFetch.
For those who are experience in this, do you know what is the root cause, and how I can resolve such?
Thanks.
I have the following HTML string. We want to render image from our app AppGroup.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title>This is title</title>
</head>
<body>
<p><h1>This is title</h1></p>
<div style="font-size: 0">
<img src="file:///Users/xxx/Library/Developer/CoreSimulator/Devices/A7B89802-9C65-4512-85A7-51C4372172D0/data/Containers/Shared/AppGroup/14DA3695-BFAF-4096-9F54-2874FD8285C2/attachment/b16c714e-9bb5-4eaa-924e-e043a69088ea.jpeg" width="100%">
</div>
This is body text
</body>
</html>
However, if we execute the following code
let html = ...
// wkWebView is WKWebView
wkWebView.loadHTMLString(html, baseURL: nil)
Only text is rendered. The image is not rendered.
May I know, what kind of configuration is required, so that WKWebView able to render image files in AppGroup directory?
Thanks.
Currently, we are implementing an undo/redo feature in UITextView.
However, we cannot use the built-in UndoManager in UITextView because we have multiple UITextView instances inside a UICollectionView.
Since UICollectionView recycles UITextView instances, the same UITextView might be reused in different rows, making the built-in UndoManager unreliable.
The shouldChangeTextIn method in UITextViewDelegate is key to implementing undo/redo functionality properly. Here is an example of our implementation:
extension ChecklistCell: UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Get the current text
let s = textView.text ?? ""
// Get the starting position of the change
let start = range.location
// Get the number of characters that will be replaced
let count = range.length
// Get the number of characters that will be added
let after = text.count
print(">>>> The current text = \"\(s)\"")
print(">>>> The starting position of the change = \(start)")
print(">>>> The number of characters that will be replaced = \(count)")
print(">>>> The number of characters that will be added = \(after)")
print(">>>>")
if let delegate = delegate, let checklistId = checklistId, let index = delegate.checklistIdToIndex(checklistId) {
delegate.attachTextAction(s: s, start: start, count: count, after: after, index: index)
}
return true
}
}
Working scene behind the UITextViewDelegate
However, this implementation does not work well with non-English input using an IME. When using an IME, there is an intermediate input before the final input is produced. For example, typing "wo" (intermediate input) produces "我" (final input). Currently, UITextViewDelegate captures both "wo" and "我".
UITextViewDelegate captures both "wo" and "我"
Is there a way to ignore the intermediate input from IME and only consider the final input?
In Android, we use the beforeTextChanged method in TextWatcher to seamlessly ignore the intermediate input from IME and only consider the final input. You can see this in action in this
Android captures only "我"
Is there an equivalent way in iOS to ignore the intermediate input from IME and only take the final input into consideration?
Currently, this is how I implement the drag and move operation:
collectionView.beginInteractiveMovementForItem
collectionView.updateInteractiveMovementTargetPosition
collectionView.endInteractiveMovement
The outcome looks like the following:
However, what I would like to achieve is the ability to customize the view of the "drop" location.
For instance, in the following example, a red line is drawn at the target drop location:
In this example, a transparent rectangle is drawn at the target drop location:
May I know how these apps achieve such an effect?
Thanks.
Google has a very strict policy regarding "associated previously terminated accounts." This means that if you are associated with another previously banned developer, you will be banned too.
For instance : https://www.reddit.com/r/androiddev/comments/9mpyyi/google_play_developer_account_terminated_due_to/
Does Apple have a similar policy?
Recently, I was considering providing read-only access to an external party on App Store Connect. However, I am concerned that it might trigger the same risk.
Therefore, I am wondering if Apple has such a policy.
Thanks.
The following code, will create a red color text, without strike-through.
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let text = "Hello World"
let textCount = text.count
let fullRange = NSRange(location: 0, length: textCount)
var attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(.foregroundColor, value: UIColor.green, range: fullRange)
attributedText.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: fullRange)
label.attributedText = attributedText
attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(.foregroundColor, value: UIColor.red, range: fullRange)
attributedText.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: fullRange)
label.attributedText = attributedText
}
}
However, if I trigger label.text in between, it will cause the following strange behavior : A red color text, with strike-through created at the end of function.
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let text = "Hello World"
let textCount = text.count
let fullRange = NSRange(location: 0, length: textCount)
var attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(.foregroundColor, value: UIColor.green, range: fullRange)
attributedText.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: fullRange)
label.attributedText = attributedText
// Why this will cause a red color text, with strike-through created at the end of function?
label.text = text
attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(.foregroundColor, value: UIColor.red, range: fullRange)
attributedText.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: fullRange)
label.attributedText = attributedText
}
}
Does anyone what is the reason behind this behavior, and how I can avoid such? Thank you.
Topic:
UI Frameworks
SubTopic:
UIKit
I am following the guideline at
https://developer.apple.com/documentation/apple_search_ads/implementing_oauth_for_the_apple_search_ads_api#3733114
However, I am getting an empty page like this :
I do not find a way upload my public key. Can someone help? Thanks.